扩展 BaseAdapter 中的警报对话框 class
Make alert dialog in BaseAdapter extended class
我有一个 MyListAdapter Java class,如下所示:
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)
当我的 class 从 AppCompatActivity
扩展时我没有任何问题
问题是您试图将 this
作为上下文传递,但它指向您的 View.OnClickListener
。
替换:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
与:
AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());
我有一个 MyListAdapter Java class,如下所示:
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)
当我的 class 从 AppCompatActivity
扩展时我没有任何问题问题是您试图将 this
作为上下文传递,但它指向您的 View.OnClickListener
。
替换:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
与:
AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());