Skip to content Skip to sidebar Skip to footer

Custom Layout With Checkbox In Listview: When I Delete A Line If It Was Checked(checkbox = True) The Next Line Gets The Check

When I delete a line if it was checked(checkBox = true) the next line gets the check. I have few lines, second line checked third NOT checked. I delete second line, and then the th

Solution 1:

At first, uncheck the CheckBox then perform the deletion of the row item.

Solution 2:

UPDATE:

Mainly I think this problems is just a regular programming problem, not specific to Android. The idea is to use SharedPreferences and to save state. OR you save it in TodoTask and get state from there. It's up to you.

What you need is a way to identify each checkbox. In your scenario I would take an id from your TodoTask. Create one if you don't have one, attach it to the TodoTask object. This you would do on your TodoTask list creation. When you loop through TodoTask list in getView, you can just get

String anIdForThisCheckbox = data.get(position).getId()

since each TodoTask has a corresponding checkbox.

In your getView()

...
finalSharedPreferencespref= PreferenceManager.getDefaultSharedPreferences(context);
booleanisChecked= pref.getBoolean(anIdForThisCheckbox,  false);
cb.setChecked(isChecked );
 ...

in your onClickListener:

cb.setOnClickListener(newView.OnClickListener() {

    @OverridepublicvoidonClick(View v) {
        Editoreditor= pref.edit();

        if (cb.isChecked()) {//Selected
            editor.putBoolean(anIdForThisCheckbox, true);//save state 

        } else {
            editor.putBoolean(anIdForThisCheckbox, false);
        }
    }
    editor.apply/commit
});
...

OLD: I think @hamid-shatu gave a good answer. Another solution would be to save away checkbox state as soon as user checks/unchecks and read status of that checkbox when you fetch it from holder. More work but if your chekbox state should survive orientation change or if you want to restore state after app has been killed, this is the way to go.

Solution 3:

1) first you need to create one class to fill listView and the element of the class should be whatever you want to display in list item and declare one integer ,than generate getter and setter method for it LIKE:

publicclassDemo{

  int state;

  publicintgetState() {

    return state;
  }

  publicvoidsetState(int state) {

    this.state = state;
  }

}

2) And then you need to fill List View with Array List of class ArrayList<Demo> ,and just maintain click when you uncheck check-box than you have to setState value to 0 by calling setter method and when you check the check-box then setState value to 1 by Calling the same setter method and call notifyDatasetChanged() method in your Adapter, And Main thing you have to do this all code in your adapter...

3) So, now when you will want to delete any list-item from list-view the procedure will be LIKE: First you will get position when you click on list-item, than you have to get class form arraylist for that position and Remove it from ArrayList , and again call notifyDatasetChanged() method in Adapter.

Solution 4:

After some more investigation i found the problem:

In my adapter code: This code need to be changed:

finalTodoTaskcurrentTask= data.get(position);
finalCheckBoxstatus= (CheckBox) holder.status;
status.setOnClickListener(newView.OnClickListener() {
  @OverridepublicvoidonClick(View v) {
    changeColorOnCheckBox(holder, status, currentTask);
  }
});

To this code:

finalTodoTaskcurrentTask= data.get(position);
finalCheckBoxstatus= (CheckBox) holder.status;

status.setChecked(currentTask.isChecked());
status.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
        changeColorOnCheckBox(holder, status, currentTask);
    if(status.isChecked()){
        Log.w("----------", "CheckboxT");
    currentTask.setChecked(true);
    }
    else{
        Log.w("----------", "CheckboxF");
    currentTask.setChecked(false);
    }
    }
});

This will update the actual line's check box that is being pressed.

Big thanks for all who was trying to help! Very appreciated!

Post a Comment for "Custom Layout With Checkbox In Listview: When I Delete A Line If It Was Checked(checkbox = True) The Next Line Gets The Check"