无法在阵列适配器中创建警报对话框

时间:2014-12-22 04:15:06

标签: android android-fragments

我有Fragment类,其中ListView由Array Adapter实现。点击ListView时,我想显示确认对话框。以下是我的Array Adapter的源代码。

Context _context;
int layoutResourceId;
List<OrderListInfo> _listOrderListInfoAdapter = new ArrayList<OrderListInfo>();
private String ButtonFlag = "";
Activity _activity;

public OrderDetailListAdatper(Context context, int Resource,
        List<OrderListInfo> listorderlistInfo) {
    super(context, Resource, listorderlistInfo);
    _context = context;
    _listOrderListInfoAdapter = listorderlistInfo;
    layoutResourceId = Resource; 

}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) _context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (row == null) {
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        TextView txtProductCode = (TextView) row
                .findViewById(R.id.txtProductCode);
Button btnDelete = (Button) row.findViewById(R.id.btnRemove);
holder.txtProductCode = txtProductCode;
row.setTag(holder);
    } else {holder = (ViewHolder) row.getTag();     }

OrderListInfo _orderListInfoAdapter = _listOrderListInfoAdapter
            .get(position);
    holder.txtProductCode.setText(_orderListInfoAdapter.getProductCode()
            .toString() 
            );

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

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

            alert.setMessage("Do you want to delete?");
                alert.setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(
                                DialogInterface dialog,
                                int whichButton) {
                            dialog.cancel();
                        }
                    });
            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(
                                DialogInterface dialog,
                                int whichButton) {

                            OrderDetailListAdatper.this._listOrderListInfoAdapter
                                    .remove(position);                          OrderDetailListAdatper.thisnotifyDataSetChanged();                                  
                        }
                    });
            AlertDialog alertDialog = alert.create();
            alertDialog.show();
        }
    });
}
    return row;
  }

调用此代码后

    (AlertDialog alertDialog = alert.create();) ,

我收到错误

     android.view.WindowManager$BadTokenException: 

无法添加窗口 - 令牌null不适用于应用程序。我该如何解决这个问题

1 个答案:

答案 0 :(得分:1)

OrderDetailListAdatper adapter = new OrderDetailListAdatper(Yourclass.this,Resource,
    listorderlistInfo);
//set your adapter..
你的getview中的

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

        AlertDialog.Builder alert = new AlertDialog.Builder((Activity)_context);

        alert.setMessage("Do you want to delete?");
            alert.setNegativeButton("CANCEL",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(
                            DialogInterface dialog,
                            int whichButton) {
                        dialog.cancel();
                    }
                });
        alert.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(
                            DialogInterface dialog,
                            int whichButton) {

                        OrderDetailListAdatper.this._listOrderListInfoAdapter
                                .remove(position);                          OrderDetailListAdatper.thisnotifyDataSetChanged();                                  
                    }
                });
        alert.create().show(); // btw show() creates and shows it..
    }
});

我认为警告对话框 - (构建器)是旧版的 - 已经过时,或者将alertdialog放在对话框片段中,或者为确认创建一个对话框片段。