CompoundButton.OnCheckedChangeListener()+ AlertDialog

时间:2015-10-02 21:19:28

标签: android checkbox alertdialog

我尝试使用警报对话框中的多个复选框,我做到了,但是当我点击一些支票时,我的代码就会繁荣!

这是我的一个活动(警告对话框),它从AppCompactActivity扩展而来,并没有实现任何东西,值得区分按钮取消和接受是否有效。

additionalBox.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater layoutInflater = LayoutInflater.from(ProductActivity.this);
            final View additionalView = layoutInflater.inflate(R.layout.dialog_additionals, null);

            // set the custom dialog components - text, buttons, accountants
            TextView titleDialog = (TextView) additionalView.findViewById(R.id.title_additional);
            titleDialog.setTypeface(boldFont);
            Button buttonAccept = (Button) additionalView.findViewById(R.id.button_accept);
            buttonAccept.setTypeface(boldFont);
            Button buttonCancel = (Button) additionalView.findViewById(R.id.button_cancel);
            buttonCancel.setTypeface(boldFont);

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ProductActivity.this);
            alertDialogBuilder.setView(additionalView);
            final AlertDialog alertD = alertDialogBuilder.create();
            alertD.setCanceledOnTouchOutside(false);
            //Fill object of additional
                ArrayList<AdditionalBean> additionalList = new ArrayList<AdditionalBean>();
                additionalList.add(new AdditionalBean("Queso", 500, false));
                additionalList.add(new AdditionalBean("Tocineta", 700, false));
                additionalList.add(new AdditionalBean("Salsas",1200,false));
                ListView additionalListView = (ListView) additionalView.findViewById(R.id.list_additional);
                additionalAdapter = new AdditionalAdapter(ProductActivity.this, additionalList);
                additionalListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                additionalListView.setAdapter(additionalAdapter);
                additionalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent,
                                            View view, int position, long id) {

                        CheckBox chk = (CheckBox) ProductActivity.this.findViewById(R.id.additional_check);
                        AdditionalBean bean = AdditionalListClass.addList
                                .get(position);
                        if (bean.isChecked()) {
                            bean.setIsChecked(false);
                            chk.setChecked(false);
                        } else {
                            bean.setIsChecked(true);
                            chk.setChecked(true);
                        }

                    }
                });
            //End of fill object of additional
            buttonCancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertD.dismiss();
                }
            });

            buttonAccept.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                   // quantity.setText(newQuantity.getText().toString());
                    alertD.dismiss();
                }
            });

            alertD.show();
        }
    });

然后这是我的适配器:

public class AdditionalAdapter extends ArrayAdapter{
private ArrayList<AdditionalBean> originalData = null;
private Context context;

public AdditionalAdapter (Context context, ArrayList<AdditionalBean> listArray){
    super(context, R.layout.additional_item);
    this.context = context;
    this.originalData = listArray ;
}

public static class Row
{
    public TextView labelName;
    public TextView labelPrice;
    public CheckBox check;
}

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

@Override
public AdditionalBean getItem(int position) {
    return originalData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
   View rowView = convertView;
    // reuse views
    if (rowView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        rowView = inflater.inflate(R.layout.additional_item, null);
        // configure view holder
        Row viewHolder = new Row();

        viewHolder.labelName = (TextView) rowView.findViewById(R.id.additional_name);
        viewHolder.labelPrice = (TextView) rowView.findViewById(R.id.additional_price);
        viewHolder.check = (CheckBox) rowView.findViewById(R.id.additional_check);

        viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton vw, boolean isChecked) {
                int getPosition = (Integer) vw.getTag();
                originalData.get(getPosition).setIsChecked(
                        vw.isChecked());

            }
        });

        rowView.setTag(viewHolder);
    }
    Row holder = (Row) rowView.getTag();
    final AdditionalBean itm = originalData.get(position);
    holder.labelName.setText(itm.getName());
    holder.labelPrice.setText("Bs. " + String.valueOf(itm.getPrice()));
    holder.check.setChecked(originalData.get(position).isChecked());
    return rowView;
}

}

具体错误是:java.lang.NullPointerException:尝试调用虚方法&#39; int java.lang.Integer.intValue()&#39;在null对象引用上             在com.xetux.x_pedidos.modules.product.AdditionalAdapter $ 1.onCheckedChanged(AdditionalAdapter.java:76)

我尝试使用this solution,但它对我不起作用

1Android Checkbox getchecked (CompoundButton.OnCheckedChangeListener (without button click event))我认为是因为警报对话框。

我想要类似的东西 this

1 个答案:

答案 0 :(得分:0)

我怀疑这一行是问题所在:int getPosition = (Integer) vw.getTag(); 我没有看到标签被设置在任何地方。 因此,您可以在绑定视图期间设置标记,或者您可以将位置变量设置为final - 这将使其可以从侦听器获得。 `