如何在列表适配器上显示 AlertDialog

How to display AlertDialog on List Adapter

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

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

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

在列表视图中,我想在用户点击列表中的任何项目时启动 Call_Action 事件。

我在 Adapter 中编写了以下代码,但它给我这个错误消息

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. 的 Context 引起的

这一行:

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

替换

context

[YourActivityName].this