I have problem with keeping checkbox state in listview. I have tried many stackoverflow questions regarding this problem. But couldnt find any solution yet. Please somebody help me. Here is my getView method in my custom adapter.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row_sub_packages, parent,
false);
TextView title = (TextView) rowView.findViewById(R.id.textView1);
TextView desc = (TextView) rowView.findViewById(R.id.textView2);
TextView sub = (TextView) rowView.findViewById(R.id.TextView01);
TextView book = (Button) rowView.findViewById(R.id.button2);
CheckBox fav = (CheckBox) rowView.findViewById(R.id.check1);
Flag.clear();
Flag = dh.getData(6, "packages");
boolean flag = false;
for (int i = 0; i < Flag.size(); i++) {
if (Flag.get(i)
.equals(SubPackagesFragment.Sub_cat_Id[position])) {
Log.i("Flag true", "Flag true " + Flag.get(i) + " "
+ SubPackagesFragment.Sub_cat_Id[position]);
flag = true;
}
else {
flag = false;
}
}
if (flag == true) {
fav.setChecked(true);
}
else {
fav.setChecked(false);
}
fav.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked == true) {
dh.addfav(Title[position], sub_cat_Sub[position],
sub_cat_des[position], Images[position],
"packages",
SubPackagesFragment.Sub_cat_Id[position]);
// Toast.makeText(getContext(),
// Title[position]+"+"+SubPackagesFragment.Sub_cat_Id[position],
// Toast.LENGTH_SHORT).show();
} else {
// Toast.makeText(getContext(), "off",
// Toast.LENGTH_SHORT).show();
dh.delete(SubPackagesFragment.Sub_cat_Id[position],
"packages");
}
}
});
title.setText(Title[position]);
return rowView;
}