lundi 2 février 2015

How to keep the state of checkBox persistent in listView

I have a fragment with a listview with 11 items, each item contains (TextView, checkbox and imageview).


the problem i am facing is, the App. is in lanscape mode, and when i check any checkbox, and scroll, the check state of the checkBox does not persist, as if it is reset.


in the below code is my attempts to keep the check state of the checkBox persistent.


I also read about the convertView, and the briefly what i understood about it is, as if the listView scrolls it does not keep its vies states it creates new object of the views with each scroll.


the code posted, does not allow any checkbox to be checked,


kindly please help me to know what i am missing and to solve this issue.


getView():



@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

final Viewholder holder;
final CheckBoxstate[] cbState = new CheckBoxstate[getCount()];
for (int i = 1; i <= getCount(); i++) {
cbState[i-1] = new CheckBoxstate();
}

if (convertView == null) {
holder = new Viewholder();

LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutinflater.inflate(R.layout.list_items_layout, null);

holder.mTextView = (TextView) convertView.findViewById(R.id.tvlist_topic);
holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.cbList_hook);
holder.mImageView = (ImageView) convertView.findViewById(R.id.ivList_delete);

convertView.setTag(holder);

} else {
holder = (Viewholder) convertView.getTag();
}

if (this.checkedItemsList == null) {
this.checkedItemsList = new ArrayList<String>();
}

holder.mTextView.setText(this.designList.get(position).getTopic());

holder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
cbState[position].setPosition(position);
cbState[position].setState(true);
holder.mCheckBox.setChecked(true);
designList.get(position).setIsChecked(true);

for (int i = 1; i <= getCount(); i++) {
if ( cbState[i-1].isChecked() ) {

} else {
holder.mCheckBox.setChecked(false);
designList.get(i-1).setIsChecked(false);
}
}

} else {

}
}
});

holder.mImageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.mCheckBox.isChecked()) {
holder.mCheckBox.setChecked(false);
designList.get(position).setIsChecked(false);
}
designList.remove(position);
notifyDataSetChanged();
}
});

return convertView;
}

private class Viewholder {
TextView mTextView;
CheckBox mCheckBox;
ImageView mImageView;
}

private class CheckBoxstate {
private int pos;
private boolean state;

public CheckBoxstate() {
// TODO Auto-generated constructor stub
}
public CheckBoxstate(int pos, boolean state) {
// TODO Auto-generated constructor stub
this.pos = pos;
this.state = state;
}

public void setPosition(int pos) {
this.pos = pos;
}
public int getPosition() {
return this.pos;
}

public void setState(boolean state) {
this.state = state;
}
public boolean isChecked() {
return this.state;
}
}




Aucun commentaire:

Enregistrer un commentaire