dragullar
dragullar

Reputation: 355

Cannot create alert dialog box in array adapter

I have Fragment class which have ListView that's implemented by Array Adapter. When click on ListView , I would like to show confirmation Dialog box . The follow is my source code for 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;
  }

after call this code

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

I get error as

     android.view.WindowManager$BadTokenException: 

Unable to add window -- token null is not for an application . How can I solve this problem

Upvotes: 1

Views: 1082

Answers (1)

Elltz
Elltz

Reputation: 10869

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

in your 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..
    }
});

also i think alert dialog-(builder) was old skulled-deprecated, either put alertdialog in a dialogfragment, or create a dialogfragment for your confirmation..

Upvotes: 1

Related Questions