Reputation: 251
I have a custom Listview each row in it consist of textview and checkbox. (1)when I click on item in list it should go to other activity and(2) when the checkbox is checked, the value in textview should be added to array//when it is unchecked the value should be removed from the array. the first requirement run successfuly with me, but the second one doesn't work. this is my code:
public class DataAdapter extends ArrayAdapter<ItemInList> {
public ArrayList<ItemInList> list;
public Activity context;
public LayoutInflater inflater;
public static ArrayList<String> array=new ArrayList<String>();
ItemInList element=new ItemInList();
public DataAdapter(Activity context,int x,ArrayList<ItemInList> list) {
super(context, 0, list);
this.context = context;
this.list = list;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
protected TextView name,Description;
protected CheckBox checkbox;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public ItemInList getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row1, null);
holder.name = (TextView) convertView.findViewById(R.id.food_title);
holder.name.setTextColor(Color.BLACK);
holder.Description = (TextView) convertView.findViewById(R.id.food_description);
holder.Description.setTextColor(Color.GRAY);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.add_food_item);
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
element = (ItemInList) holder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
if(element.isSelected())
{
array.add(element.getName());
}
else
{
array.remove(position);
}
}
});
convertView.setTag(holder);
} else {
holder=(ViewHolder)convertView.getTag();
ItemInList bean = (ItemInList) list.get(position);
holder.name.setText( bean.getName());
holder.Description.setText( bean.getDescription()+"");
holder.checkbox.setChecked( bean.isSelected());
return convertView;
}
holder.name.setText(list.get(position).getName());
holder.Description.setText(list.get(position).getDescription()+"");
holder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
the errors:
null pointer exception..
at DataAdapter$1.onCheckedChanged(DataAdapter.java:92)
E/AndroidRuntime(543): at android.widget.CompoundButton.setChecked(CompoundButton.java:125)
E/AndroidRuntime(543): at sehaty.com.DataAdapter$1.onCheckedChanged(DataAdapter.java:92)
E/AndroidRuntime(543): at android.widget.CompoundButton.setChecked(CompoundButton.java:125)
at android.widget.CompoundButton.performClick(CompoundButton.java:99)
any help will be appreciated
Upvotes: 1
Views: 4219
Reputation: 3804
As I already stated in comments: you try to get the tag from your checkbox but you never set the tag. So element
must be null. Thus you get a NullPointer. The question is..why do you try to get the tag? There is no need using that method (if i understand your intention correctly). You want to access your element
variable for the corresponding postion. That element is in your list so you can try the following (the code is not tested..just copied from you and made some changes):
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row1, null);
holder.name = (TextView) convertView.findViewById(R.id.food_title);
holder.name.setTextColor(Color.BLACK);
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.add_food_item);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ItemInList element = list.get(position);
holder.name.setText(element.getName());
holder.checkbox.setChecked(element.isSelected());
holder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
element.setSelected(buttonView.isChecked());
if (element.isSelected()) {
array.add(element.getName());
} else {
if (position < array.size())
array.remove(position);
}
}
});
return convertView;
}
Upvotes: 1
Reputation: 13552
In the following line
element = (ItemInList) holder.checkbox.getTag();
you are trying to get a Tag from the checkbox, which is not beng set anywhere in the getView(). As a result it returns null.
Upvotes: 0
Reputation: 39460
Check line 92 in your DataAdapter class. It looks like the adapter (or other var?) is null. Have you instantiated all objects? If this doesn't help, please post your full code including XML.
Upvotes: 0