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

标签 java android listview android-studio android-arrayadapter

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/

相关文章:

android - Android 中的委托(delegate)(常见)点击监听器

android - 从 ListView 中删除项目时出现 IndexOutofBound 错误

java - "synchronized-with-resource"获取锁对象也必须同步怎么办?

java - 从android中的url获取XML

android - 更新应用程序是否会清除共享首选项或删除应用程序设置的警报?

c# - Listitem 添加到 c# 中的第二行

android - 如何根据升序整数对sqlite表进行排序?

java - 使用LLList的代码中的错误

java - Jsp类不可见

android - MediaPlayer.setDataSource 导致有效文件的 IOException