у меня есть этот код в моей MainActivity (), который используется для подсчета итоговых записей и его чтения.
public void countRecords() {
int recordCount = new TableControllerStudent(this).count();
TextView textViewRecordCount = (TextView) findViewById(R.id.textViewRecordCount);
textViewRecordCount.setText(recordCount + " records found.");
}
public void readRecords() {
LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
linearLayoutRecords.removeAllViews();
List<ObjectStudent> students = new TableControllerStudent(this).read();
if (students.size() > 0) {
for (ObjectStudent obj : students) {
int id = obj.id;
String studentFirstname = obj.firstname;
String studentEmail = obj.email;
String textViewContents = studentFirstname + " - " + studentEmail;
TextView textViewStudentItem= new TextView(this);
textViewStudentItem.setPadding(0, 10, 0, 10);
textViewStudentItem.setText(textViewContents);
textViewStudentItem.setTag(Integer.toString(id));
linearLayoutRecords.addView(textViewStudentItem);
}
}
else {
TextView locationItem = new TextView(this);
locationItem.setPadding(8, 8, 8, 8);
locationItem.setText("No records yet.");
linearLayoutRecords.addView(locationItem);
}
}
, а в Activity OnListenerCreateStudent он не работает и выдает ошибку с этим кодом.
package com.example.coba_crud_db_1;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
public class OnClickListenerCreateStudent implements View.OnClickListener {
@Override
public void onClick(View view) {
final Context context = view.getRootView().getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.student_input_form, null, false);
final EditText editTextStudentFirstname = (EditText) formElementsView.findViewById(R.id.editTextStudentFirstname);
final EditText editTextStudentEmail = (EditText) formElementsView.findViewById(R.id.editTextStudentEmail);
new AlertDialog.Builder(context)
.setView(formElementsView)
.setTitle("Create Student")
.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String studentFirstname = editTextStudentFirstname.getText().toString();
String studentEmail = editTextStudentEmail.getText().toString();
ObjectStudent objectStudent = new ObjectStudent();
objectStudent.firstname= studentFirstname;
objectStudent.email= studentEmail;
boolean createSuccessful = new TableControllerStudent(context).create(objectStudent);
if(createSuccessful){
Toast.makeText(context, "Student information was saved.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Unable to save student information.", Toast.LENGTH_SHORT).show();
}
((MainActivity)context).countRecords();
dialog.cancel();
}
}).show();
}
}
Журнал ошибок:
2020-01-31 11:39:18.357 12917-12917/com.example.coba_crud_db_1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.coba_crud_db_1, PID: 12917
java.lang.ClassCastException: com.android.internal.policy.DecorContext cannot be cast to com.example.coba_crud_db_1.MainActivity
at com.example.coba_crud_db_1.OnClickListenerCreateStudent$1.onClick(OnClickListenerCreateStudent.java:46)
at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
2020-01-31 11:39:18.417 12917-12917/com.example.coba_crud_db_1 I/Process: Sending signal. PID:
12917 SIG: 9
проблема в ((MainActivity) контексте) .countRecords (); и ((MainActivity) context) .readRecords (); Я полагаю. я пробовал другой код, но он не работает, он приближается, если я добавляю новые записи.