适配器类中的AlertDialog出错

时间:2017-09-19 20:27:40

标签: android listview

我有一个在适配器的onclick上调用的代码......

string player1 = PlayerList[0];
string player2 = PlayerList[1];
// and so on for the remaining players

当我运行此代码时,第一行代码出错。这一个...

final AlertDialog.Builder builder = new AlertDialog.Builder(
                context);
        builder.setMessage("Disconnect from Instagram?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            //do something
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                mApp.resetAccessToken();
                                // btnConnect.setVisibility(View.VISIBLE);

                                // tvSummary.setText("Not connected");
                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                dialog.cancel();
                            }
                        });
        final AlertDialog alert = builder.create();
        alert.show();

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

'上下文'必须是活动的类型

答案 1 :(得分:0)

尝试将第一行更改为:

final AlertDialog alertDialog = new AlertDialog.Builder(ActivityName.this).create();

另外,删除底部的这一行:

final AlertDialog alert = builder.create();

如果这不起作用,请将代码发布到调用此代码的适配器中。

如果您在片段内,请使用此片段:

final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();

答案 2 :(得分:-1)

public boolean onLongClick(View v) {
    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getRootView().getContext())
            .setMessage("Do you really want to delete this size")
            .setCancelable(false)
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
}