在BaseAdapter扩展类中创建警报对话框

问题描述 投票:-4回答:1

我有一个MyListAdapter Java类,如下所示:

class MyListAdapter extends BaseAdapter implements TextToSpeech.OnInitListener{

现在我想对每个列表项进行确认对话,如下所示:

btnDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {




            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Delete");
            alert.setMessage("Are you sure?");
            alert.setPositiveButton(android.R.string.yes,
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                            // continue with delete
                new DatabaseHelper(context).deleteEmployee(employees.get(position));
                employees.remove(position);
                notifyDataSetChanged();
                        }
                    });

但是

AlertDialog.Builder alert = new AlertDialog.Builder(this);

我有一个错误,它说:

builder (android.content.Context)    in Builder cannot be applied to (anonymous android.view.View.OnClickListner)

当我的类从AppCompatActivity扩展时,我没有任何问题

android alertdialog baseadapter appcompatactivity
1个回答
0
投票

问题是你试图将this作为一个上下文,但它指向你的View.OnClickListener

更换:

AlertDialog.Builder alert = new AlertDialog.Builder(this);

附:

AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());
© www.soinside.com 2019 - 2024. All rights reserved.