Suppose I check an item's checkbox, that also updates the ischecked column in the database with the respective item's id. Then I scroll down and go back to the top, then the top most item's checkbox is automatically unchecked and database is also updated. Even suppose I check two items on the top, when I scroll down the two bottom most items' checkboxes are also checked automatically. How can I resolve this problem in recyclerView?
// costume adapter class
public class DataBeanAdapter extends RecyclerView.Adapter<DataBeanAdapter.ViewHolder> implements View.OnCreateContextMenuListener {
public List<dataBean> items;
public int itemLayout;
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo contextMenuInfo) {
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title
menu.add(0, v.getId(), 0, "SMS");
}
public DataBeanAdapter(List<dataBean> items, int itemLayout) {
this.items = items;
this.itemLayout = itemLayout;
}
@Override
public DataBeanAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// On loading the activity recyclerview, setting properties to each list items
// fetch id with the given item postition
Cursor cc = myDB.getCursorByPosition(position);
String idd = cc.getString(cc.getColumnIndex("_id"));
String taskval = myDB.getTaskValue(idd);
dataBean item = items.get(position); // trash
holder.tasks.setText(taskval.trim());
String iscGet = myDB.getischecked(idd); // false by default
if (items.get(position).getChecked() == true) {
holder.isc.setChecked(true);
items.get(position).setChecked(true);
} else if (items.get(position).getChecked() == false) {
holder.isc.setChecked(false);
items.get(position).setChecked(false);
} else {
//leave
}
// set inflated checkbox to null
// holder.isc.setOnCheckedChangeListener(null);
// holder.isc.setChecked(myAdapter.items.contains(items.get(position)));
holder.isc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Boolean ischecked = items.get(position).getChecked();
if (ischecked == false) {
Cursor cx = myDB.getCursorByPosition(position);
String idx = cx.getString(cx.getColumnIndex("_id"));
myDB.updateCheck(idx);
items.get(position).setChecked(true);
} else {
Cursor cx = myDB.getCursorByPosition(position);
String idx = cx.getString(cx.getColumnIndex("_id"));
myDB.updateunCheck(idx);
items.get(position).setChecked(false);
}
}
});
holder.tasks.setOnCreateContextMenuListener(MainActivity.this);
holder.more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor c = myDB.getCursorByPosition(position);
String id = c.getString(c.getColumnIndex("_id"));
Intent intent = new Intent(MainActivity.this, edit.class);
intent.putExtra("taskpos", Integer.toString(position));
intent.putExtra("taskid", id);
startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tasks;
public CheckBox isc;
public ImageView more;
public ViewHolder(View itemView) {
super(itemView);
tasks = (TextView) itemView.findViewById(R.id.taskline);
isc = (CheckBox) itemView.findViewById(R.id.cbox);
more = (ImageView) itemView.findViewById(R.id.more);
// https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#isRecyclable()
// this.setIsRecyclable(false); /// prevents checkbox misbehaving
}
}
}
public List<dataBean> getAllData() {
List<dataBean> list = new ArrayList<>();
Cursor cursor = myDB.SelectData();
while (cursor.moveToNext()) {
int tasks = cursor.getColumnIndex("tasklist");
int ischecked = cursor.getColumnIndex("ischecked");
String tasksv = cursor.getString(tasks);
String ic = cursor.getString(ischecked);
dataBean bean = new dataBean(tasksv, Boolean.parseBoolean(ic));
list.add(bean);
}
return list;
}
Aucun commentaire:
Enregistrer un commentaire