gpt4 book ai didi

java - 使用 AlertDialog 中的上下文函数从 MainActivity 获取方法时出现问题

转载 作者:行者123 更新时间:2023-12-01 18:38:14 25 4
gpt4 key购买 nike

我的 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)context).countRecords();和 ((MainActivity)context).readRecords();我猜。我尝试了不同的代码,但它不起作用,如果我添加新记录,它就会强制关闭。

最佳答案

您能否分享您显示警报对话框的代码?此外,将上下文转换到 Activity 中也不是一个好的做法。当您拥有大型代码库并且多次使用相同的对话框时,这将是一个问题。最好创建一个接口(interface)并传递给alertdialog,并在您的 Activity/fragment 中使用接口(interface)的回调。

关于java - 使用 AlertDialog 中的上下文函数从 MainActivity 获取方法时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59997971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com