1

我想在一个使用 arrayAdapter 扩展的类中显示警报对话框。

我收到此错误

android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序

这是我的代码,我尝试过的

public class DeteteAdapter extends ArrayAdapter<Gold> {
private int contexts = Intent.FLAG_ACTIVITY_NEW_TASK;
private Context context;
private List<Gold> subjects = new ArrayList<Gold>();
private TextView subject;
private TextView date_day;
private TextView time;
private ProgressDialog pDialog;
String name;
JSONParser jsonParser = new JSONParser();
// url to create new product
private static String url_create_product = "http://ayyappagold.com/ayyappa/test.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";

public static String month;
public static String year;

public DeteteAdapter(Context context, int textViewResourceId,
        List<Gold> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    this.subjects = objects;
}

@Override
public int getCount() {
    return this.subjects.size();
}

@Override
public Gold getItem(int index) {
    return this.subjects.get(index);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
        // ROW INFLATION
        Log.d("ExamAdapter", "Starting XML Row Inflation ... ");

        LayoutInflater inflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.custom_gold, parent, false);
        Log.d("ExamAdapter", "Successfully completed XML Row Inflation!");
    }

    // Get item
    Gold text = getItem(position);
    subject = (TextView) row.findViewById(R.id.textView1);

    time = (TextView) row.findViewById(R.id.textView2);

    String content = text.content;

    String times = text.time;

    subject.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            name = getItem(position).id.toString();
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set title
            alertDialogBuilder.setTitle("Delete");

            // set dialog message
            alertDialogBuilder
                    .setMessage("This item will be deleted")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if this button is clicked, close
                                    // current activity
                                    new DeleteProduct().execute();
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }
    });





    subject.setText(content.replace("*", "\n"));

    return row;
}

请帮我在这个类中显示警报对话框

4

3 回答 3

3

我找到了答案。我真的很高兴与你分享,

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        v.getRootView().getContext());

使用v.getRootView().getContext()代替 context 或 v.getContext()

于 2013-06-11T07:39:42.317 回答
1

试试这个

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( DeteteAdapter.this);
于 2013-04-09T11:24:51.787 回答
0
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

像这样改变这条线;

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                (Activity)context);

也许它可以帮助你

于 2013-06-11T07:47:28.697 回答