The problem can be solve easily by keeping CheckBox
states. A self defined and explained complete Sample Class code for the same is as below_
public class AreaDataAdapter extends ArrayAdapter<String> {
private List<String> areaList;
private Activity context;
ArrayList<Boolean> positionArray;
public AreaDataAdapter(Context context, int textViewResourceId,
List<String> offersAreaList) {
super(context, textViewResourceId, offersAreaList);
// TODO Auto-generated constructor stub
this.context=context;
this.areaList = offersAreaList;
positionArray = new ArrayList<Boolean>(offersAreaList.size());
for(int i =0;i<offersAreaList.size();i++){
positionArray.add(false);
}
}
public AreaDataAdapter(Activity context, List<String> offersAreaList) {
super(ShowOffersActivity.this, R.layout.filterlist_row, offersAreaList);
this.context=context;
this.areaList=offersAreaList;
positionArray = new ArrayList<Boolean>(offersAreaList.size());
for(int i =0;i<offersAreaList.size();i++){
positionArray.add(false);
}
}
public View getView(final int position, View convertView,ViewGroup parent) {
View row=convertView;
FilterViewHolder holder;
if (row==null) {
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.filterlist_row, parent, false);
holder = new FilterViewHolder();
holder.filterCheckBox = (CheckBox)row.findViewById(R.id.filter_checkbox);
holder.filterCheckBox.setTypeface(fontFace);
row.setTag(holder);
} else {
//holder = (View) row;
holder = (FilterViewHolder) row.getTag();
/* When a listview recycles views , it recycles its present state as well as listeners attached to it.
* if the checkbox was checked and has a onCheckedChangeListener set, both will remain a part of
* recycled view based on position. So it is our responsibility to reset all states and remove
* previous listeners.
* The listener was removed as below:-
*/
holder.filterCheckBox.setOnCheckedChangeListener(null);
}
holder.filterCheckBox.setText(areaList.get(position));
holder.filterCheckBox.setFocusable(false);
holder.filterCheckBox.setChecked(positionArray.get(position));
holder.filterCheckBox.setText(areaList.get(position));
holder.filterCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
//change state of concern item in 'positionArray' array to 'True'
positionArray.set(position, true);
//add the checked item in to area list which used to filter offers.
filterOffersByThisAreaList.add(areaList.get(position));
} else {
//change state of concern item in 'positionArray' array to 'True'
positionArray.set(position, true);
//remove the unchecked item in to area list which used to filter offers.
filterOffersByThisAreaList.remove(areaList.get(position));
}
}
});
return row;
}
}
Custom Row (filterlist_row)
for the ListView
Containing these CheckBox
s is as below_
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="@+id/filter_checkbox"
style="@style/CodeFont"
android:button="@drawable/bg_custom_checkbox"
android:text="Indian" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#c9c9c3" />
</LinearLayout>
customize CheckBox
backgroung is as below_
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_s" android:state_checked="true"></item>
<item android:drawable="@drawable/checkbox_ns" android:state_checked="false"></item>
</selector>