I'm trying to work with multiple checkboxes inside an Alert Dialog, I made it, but when I click in some check, my code make boom!
Here is a piece of my activity (the alert dialog) that extends from AppCompactActivity and it doesn't implements nothing, it's worthy to distinguish that the button cancel and accept is working.
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();
}
});
then here is my adapter:
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;
}
}
The specific error is: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at com.xetux.x_pedidos.modules.product.AdditionalAdapter$1.onCheckedChanged(AdditionalAdapter.java:76)
I tried with this solution , but it doesn't work to me
1: Android Checkbox getchecked (CompoundButton.OnCheckedChangeListener (without button click event)) I supposed is because the alert dialog.
Aucun commentaire:
Enregistrer un commentaire