Skip to content Skip to sidebar Skip to footer

Check A List View Item From Adapter When Long Clicked

I have a listview for a note application set with my adapter. The list view component consists of some textviewsand a checkbox which pops up delete icon when a listviewitem is long

Solution 1:

The problem here is you have set the visibility of the checkBox from only one holder. Your logic is right but you haven't changed the visibility of checkBoxes from all the holders. You have to create a arrayList for holders and initialize them with null items.

private ArrayList<View> collectionOfViews = new ArrayList<>();
for (int i = 0; i < objects.size(); i++) {
   collectionOfViews.add(null);
}

This is my getView() from the adapter. Also to store checkStatus from all the boxes add a element checkStatus in your Note object.

if (note != null) {
        finalTextViewtextView= convertView.findViewById(R.id.textView);
        finalCheckBoxcheckBox= convertView.findViewById(R.id.checkBox);              
        checkBox.setChecked(note.getCheckStatus());
        textView.setText(note.getTextView());
        convertView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                if (checkBox.getVisibility() != View.GONE) {
                    note.changeCheckStatus();
                    checkBox.setChecked(note.getCheckStatus());
                }
            }
        });
        checkBox.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                note.changeCheckStatus();
                checkBox.setChecked(note.getCheckStatus());
            }
        });
        //setting holder in your array
        collectionOfViews.set(position, convertView);
        convertView.setOnLongClickListener(newView.OnLongClickListener() {
            @OverridepublicbooleanonLongClick(View v) {
                note.changeCheckStatus();
                checkBox.setChecked(note.getCheckStatus());
                returntrue;
            }
        });
        checkBoxStatus();
        checkBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
            @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkBoxStatus();
            }
        });
}

The visibility of the checkBox is handled from checkBoxStatus()

voidcheckBoxStatus(){
    intcount=0;
    for (Note noteCheck : mData) {
        if (!noteCheck.getCheckStatus()) {
            count++;
        }
    }
    if (count == mData.size()) {
        for (View view : collectionOfViews) {
            if (view != null) {
                CheckBoxcheckBoxInside= view.findViewById(R.id.checkBox);
                checkBoxInside.setVisibility(View.GONE);
            }
        }
    }else{
        for (View view : collectionOfViews) {
            if (view != null) {
                CheckBoxcheckBoxInside= view.findViewById(R.id.checkBox);
                checkBoxInside.setVisibility(View.VISIBLE);
            }
        }
    }
    notifyDataSetChanged();
}

Read this article this may help you. I got this idea of storing views in array from here.

I hope this helps.

Solution 2:

Try the code below:

Adapter:

classNoteListAdapterextendsArrayAdapter<Note>{
private List<Note> objects;
private List<Note> originalList = newArrayList<>();
boolean isLongPressed;
//    boolean isChecked;boolean[] isChecked;

NoteListAdapter(Context context, int resource, List<Note> objects) {
    super(context, resource, objects);
    this.objects = objects;
    this.originalList.addAll(objects);
    isLongPressed = false;
//        isChecked = false;
    isChecked = newboolean[objects.size()];
    for(int i=0; i<isChecked.length; i++) isChecked[i] = false;
}

@OverridepublicintgetCount() {
    return objects.size();
}

@Nullable@Overridepublic Note getItem(int position) {
    return objects.get(position);
}

@OverridepubliclonggetItemId(int position) {
    return position;
}

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_component, parent, false);
    }

    Notenote= getItem(position);
    if (note != null) {
        TextViewtitle= convertView.findViewById(R.id.list_note_title);
        TextViewcontent= convertView.findViewById(R.id.list_note_content_preview);
        TextViewdate= convertView.findViewById(R.id.list_note_date);
        // setting checkbox logic on the adapterCheckBoxcheckBox= convertView.findViewById(R.id.checkbox);
        // now i wanna toggle checked items from a checkbox on my header//  if (isItemsChecked) {//           checkBox.setChecked(true);//       } else {//           checkBox.setChecked(false);//       }if (isLongPressed) {
            checkBox.setVisibility(View.VISIBLE);
        } else {
            checkBox.setVisibility(View.GONE);
        }
        // also handle checks for all list view items
        checkBox.setChecked(isChecked[position]);
        checkBox.setTag(position);
        checkBox.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                CheckBoxcb= (CheckBox) v;
                intcheckedPosition= (int)cb.getTag();
                isChecked[checkedPosition] = cb.isChecked();
                notifyDataSetChanged();
            }
        });
    }
    return convertView;
}

voidshowCheckbox(int clickedPosition) {
    isLongPressed = true;
    for(int i=0; i<isChecked.length; i++) isChecked[i] = false;
    isChecked[clickedPosition] = true;
    notifyDataSetChanged();  // Required for update
}

voidremoveCheckbox() {
    isLongPressed = false;
    notifyDataSetChanged();  // Required for update
}

voidcheckboxAllChange(boolean state) {
    isLongPressed = true;
    for(int i=0; i<isChecked.length; i++) isChecked[i] = state;
    notifyDataSetChanged();  // Required for update
}

//    void Check() {//        isChecked = true;//        notifyDataSetChanged();  // Required for update//    }//    void Uncheck() {//        isChecked = false;//        notifyDataSetChanged();  // Required for update//    }

}

In Activity:

    mListNotes.setOnItemLongClickListener(newAdapterView.OnItemLongClickListener() {
        @OverridepublicbooleanonItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            deleteButtonIn();
            na.showCheckBox(position);
            returntrue;
        }
    });
    CheckboxuniversalCheckBox= findViewById(R.id.check_all);
    universalCheckBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
        @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            na.checkboxAllChange(buttonView.isChecked());
        }
    });

Post a Comment for "Check A List View Item From Adapter When Long Clicked"