gpt4 book ai didi

java - 如何在列表适配器上显示警报对话框

转载 作者:行者123 更新时间:2023-12-01 21:28:17 24 4
gpt4 key购买 nike

ContactsDataList是一个数组列表,包含所有联系人的数据。

我编写了以下将数据传递到列表适配器的代码

adapter= new ContactsAdapter(getApplicationContext(), contactsDataList);
listView = (ListView) findViewById(R.id.lv_contacts);
listView.setAdapter(adapter);

在 ListView 中,每当用户单击列表中的任何项目时,我想启动 Call_Action 事件。

我在适配器中编写了以下代码,但它给了我此错误消息

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

contactsHeading.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {

AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Are you sure you want to call "+contactsHeading + "?" );
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contactsText));
if (ActivityCompat.checkSelfPermission(v.getContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

return;
}
v.getContext().startActivity(intent);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();
}
});
alertDialog.show();

请指导我如何解决此错误消息。

最佳答案

这是由您传递到AlertDialog.Builder上下文引起的。

在这一行中:

AlertDialog alertDialog = new AlertDialog.Builder(context).create();

替换

context

[YourActivityName].this

关于java - 如何在列表适配器上显示警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37757342/

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