Reputation: 21
I created a Listview with Textview and Checkbox. Firstly I keep all item is Checked default.
But when I uncheck the checkbox and scroll it down to uncheck some other items in the list view, the older ones are checked. Please help me with my code.
This is my code for Adapter Class.
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
try {
ViewHolder holder = new ViewHolder();
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = layout_inflater.inflate(R.layout.dashbordmenu_listadapter, null);
holder.txtMenutitle = (TextView) convertView.findViewById(R.id.txtPersonList);
holder.checkAppList = (CheckBox) convertView.findViewById(R.id.checkPersonList);
convertView.setTag(holder);
convertView.setTag(R.id.txtPersonList, holder.txtMenutitle);
convertView.setTag(R.id.checkPersonList, holder.checkAppList);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkAppList.setTag(position);
holder.txtMenutitle.setText(arrayListDashboard1.get(position).getMenuTitle());
holder.checkAppList.setChecked(arrayListDashboard1.get(position).isSelected());
holder.checkAppList.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int selectPosition = (int) buttonView.getTag();
if(buttonView.isChecked())
{
selectedAppId = arrayListDashboard1.get(selectPosition).getMenuId();
Log.d("TEST","selectedAppId = "+selectedAppId);
} else {
notSelectedAppId = arrayListDashboard1.get(selectPosition).getMenuId();
Log.d("TEST","notSelectedAppId = "+notSelectedAppId);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
public class ViewHolder {
TextView txtMenutitle;
public CheckBox checkAppList;
}
Upvotes: 0
Views: 1084
Reputation: 39
Set a boolean variable 'isCheck' to your model class and set it as default false.change this according to checkbox value changed as true or false. and also check in checkbox change event isCheck is true or false.
Upvotes: 0
Reputation: 13617
First Method:
Add a boolean field to your model. set it as default true. if checkbox unchecked make the boolean field as false. update your checkbox according to the boolean field.
Second Method:
using SparseBooleanArray, In your adapter initialize SparseBooleanArray. It will hold the positon and boolean value. So you can keep your check box value.
SparseBooleanArray Document:
Unlike a normal array of booleans, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Booleans, both because it avoids auto-boxing keys and values and its data structure doesn't rely on an extra entry object for each mapping.
Third Method:
As LunarWatcher Said, Simply Initialize ArrayList of Booleans and update your check boxes.
Upvotes: 1
Reputation: 45
RecylerView
re-uses views while scrolling so you'll have to main the state of the checkbox for each list item. In BindViewHolder
you'll check the state for the position and set the state of the checkbox accordingly.
Upvotes: 0
Reputation: 695
In you model take one Boolean to keep record of check box whether its checked or not and use it while binding views
Upvotes: 0