在BaseAdapter扩展类

时间:2018-03-02 20:52:30

标签: android alertdialog baseadapter appcompatactivity

我有一个类似下面的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扩展时,我没有任何问题

1 个答案:

答案 0 :(得分:0)

问题是您尝试将this作为上下文传递,但它指向您的View.OnClickListener

替换:

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

使用:

AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());