Currently i have a checkbox on my listview,from the BaseAdapter i have set the checkbox id with my own id.
When i access this id from the Activity . I get null pointer exception? Though i debug i still have that null pointer exception?
Is it not possible to set id of the checkbox manually. Checkbox does have a method
setId(int id).
Any thoughts on null pointer exception on checkBox.getId()?
this is my getView() where i have set the checkBox id.
viewHolder.checkBox.setId(myList.get(position).getId());
On Activity i have done as this because i have to delete all the checked CheckBox.
private void deleteCheckItems() {
SparseBooleanArray booleanArray = listView.getCheckedItemPositions();
for (int i = 0; i < listView.getCount(); i++) {
if (booleanArray.get(i))
Toast.makeText(getBaseContext(), " the selected index is " + i, Toast.LENGTH_SHORT).show();
}
}
I am getting null pointer exception from this method
Edited: My Source Code is as:
public View getView(int position, View view, ViewGroup parent){
ViewHolder viewHolder;
if(view==null){
viewHolder = new ViewHolder();
view = layoutInfalter.inflate(R.layout.item_row,null);
viewHolder.checkBox = (CheckBox)view.findViewById(R.id.CheckBoxId);
if(VIEW_MODE==-1){
viewHolder.checkBox.setVisibility(View.INVISIBLE);
}else{
myAdapter.setViewMode(1);
viewHolder.checkBox.setVisibility(View.VISIBLE);
viewHolder.checkBox.setId(myList.get(position).getId());
}
viewHolder.text1= (TextView)view.findViewById(R.id.text1);
viewHolder.text2 =TextView)view.findViewById(R.id.text2);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.text1.setText(myList.get(position).getText1());
viewHolder.text2.setText(myList.get(position).getText2());
//viewHolder.checkBox.setId(myList.get(position).getId());
return view;
}
this might help you!!
It works for me:
ROW.xml
In the above xml change ImageView to CheckBox. and edit your getView like This:
GetView method in your baseAdapter:
In the above getView method Do Change the ImageView to checkbox and refer it to the corresponding checkbox id in your row.xml Also implement your BaseAdapter Class with onCLickListener so that you would be able to get the onClick method in that base adapter class.
Hope you get my point. For any confusion Ask ME..
Thanks
sHaH