I have a ListView adapter where I am writing the ListItems from an XML. I want to flag when an item is checked and unflag it when an item is unchecked. I am using an array for storing the flag values. For Example if my ListView has four list items “Red, Blue, Green, Yellow”. If Red and Green are checked, the array should be updated to [1, 0, 1, 0] and if they are unchecked it should be updated to [0, 0, 0, 0]. Below is my code. I did something wrong and it is not displaying the desired output. Can someone look into it and suggest me a better way to solve it. Thank you!
ArrayAdapter<String> adapter;
array = getResources().getStringArray(R.array.facilities);
strings1 = new ArrayList(Arrays.asList(array));
TopicSelectionListAdapter topicSelectionListAdapter = new TopicSelectionListAdapter(
second.this, R.layout.listrow, strings1);
listView.setAdapter(topicSelectionListAdapter);
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
for(int i=0;i<strings1.size();i++){
listView.setItemChecked(i, false);
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
CheckedTextView selectedItem = (CheckedTextView) view;
boolean isChecked = selectedItem.isChecked();
Log.e("TAG","item clicked position = " + position + " isChecked = " + isChecked);
for(int i=0; i< strings1.size(); i++){
if(!isChecked){
bArray[i]= 0;
}
else{
bArray[i]= 1;
}
}
for(int i =0; i < bArray.length; i++){
Log.e("TAG","boolean Array value = " + bArray[i]);
}
}
});
this part simply ticks each part of your
bArraythe reason for this is, you simply wiping your whole array without checking if the element of
bArrayfor this particular event checked or not…doing this should result something better: