Reputation: 377
I know there are many solutions provided to this issue, But I am not exactly the proper solution to this, when a checkbox is checked I am assigning a ID to it,and adding it to a array, but when scroll down, the checkbox gets unchecked ,but the ID is stored in array,I want to keep the checkbox checked if it is checked.
My adapter class is.
public class sendivitesadapter extends ArrayAdapter<Item> implements Filterable,SectionIndexer{
private Context context;
private ArrayList<Item> items;
private qrusers qrusers;
private LayoutInflater vi;
private String[] udis;
private final boolean[] mCheckedState;
qrusers qrus;
private ItemsFilter mFilter;
public sendivitesadapter(Context context,ArrayList<Item> items,String[]udis) {
super(context, 0,items);
this.context = context;
mCheckedState = new boolean[items.size()];
this.qrusers =(qrusers) context;
this.items = items;
this.udis=uid;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Item getItem(int position) {
return super.getItem(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
sendItem ei = (sendItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
final TextView title = (TextView)v.findViewById(R.id.contactname);
final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
//checkBox.setTag(position);
//items.addAll(uid);
checkBox.setTag(udis[position]);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
s = (String)buttonView.getTag();
Log.e("IDDDDDDDD", s);
userid.add(s);
Log.e("Array", userid.toString());
} else {
s = (String)buttonView.getTag();
userid.remove(s);
}
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(qrusers.this);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("userid", TextUtils.join(",", userid));
editor.commit();
}
});
// Log.e("IDDDDDDD", text);
//checkBox.setTag("12");
/* checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(checkBox.isChecked()){
s= (String)v.getTag();
Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
userid.add(s);
Log.e("Array", userid.toString());
}
else{
s=(String)v.getTag();
userid.remove(s);
}
}
});*/
if (title != null)
title.setText(ei.contactname);
if(subtitle != null)
subtitle.setText(ei.companyname);
}
}
return v;
}
Upvotes: 0
Views: 1157
Reputation: 502
This question has your answer. You have to maintain a separate structure to check/uncheck the checkboxes.
ListView Viewholder checkbox state
Upvotes: 0
Reputation: 13761
If you want to store the status of the checkboxes, you'll have to update it in your internal ListView or whatever structure you're using and what seems to be called items
in your extended adapter, and don't forget to call notifyDataSetChanged()
afterwards.
Upvotes: 1