Search code examples
androidlistviewcheckboxbaseadapternotifydatasetchanged

listview is not refreshing values after deleting row with checkbox value = true


Good day,

I'm doing a listview, baseadapter with checkbox that is able to delete multiple selected rows.

here is my onCreate :

 ArrayList<Memos> list;

 list = new ArrayList<Memos>();
 list.add(new Memos(1, "s", "s"));
 list.add(new Memos(2, "x", "aaa"));
 list.add(new Memos(3, "v", "aesf"));

 final ListView lv = (ListView) findViewById(R.id.myList);
 lv.setAdapter(new MemoListAdapter(list, this));

 deletebutton
 @Override
      public void onClick(View v) {
           MemoListAdapter myAdapter = (MemoListAdapter)lv.getAdapter();  
           myAdapter.remove();
      }

then here is my complete baseadapter :

        public class MemoListAdapter extends BaseAdapter {
    private List<Memos> listComment;
    private Context context;
    private LayoutInflater inflater = null;
    private ArrayList<Memos> deleteMemos;

    public MemoListAdapter(List<Memos> listComment, Context context) {
        super();
        this.listComment = listComment;
        this.context = context;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        deleteMemos = new ArrayList<Memos>();
    }

    @Override
    public int getCount() {

        return listComment.size();
    }

    @Override
    public Memos getItem(int position) {
        return listComment.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {

     return getCount();
    }

    @Override
    public int getItemViewType(int position) {

     return position;
    }

    public class ViewHolder
    {
        TextView body;
        TextView date;
        CheckBox checkBox;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {   
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_view, null);
            holder = new ViewHolder();
            holder.body = (TextView) convertView.findViewById(R.id.big_text);
            holder.date = (TextView) convertView.findViewById(R.id.small_text);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            holder.body.setText(listComment.get(position).getMessageBody());
            holder.date.setText(listComment.get(position).getMessageDate());

            holder.checkBox.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                          //is chkIos checked?
                    if(listComment.get(position).isCheckbox()) {
                     holder.checkBox.setChecked(false);
                     deleteMemos.remove(listComment.get(position));
                    }
                    else {
                        // Do invisible or gone stuff here
                     holder.checkBox.setChecked(true);
                     deleteMemos.add(listComment.get(position));
                    }

                }
              });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    public void remove() {

        for(Memos memo : deleteMemos) {
            listComment.remove(memo);
        }
        this.notifyDataSetChanged();
    }
    }

after selecting rows to delete (4 rows)

enter image description here

the list adjusted but the contents and the check value of the checkbox is still the same.

enter image description here

I am wondering what part of the notifyDataSetChanged is wrong. Thanks in advance!


Solution

  • Your views are reused that's why it happen . Try adding below codes into your getView()

     @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_view, null);
                holder = new ViewHolder();
                holder.body = (TextView) convertView.findViewById(R.id.big_text);
                holder.date = (TextView) convertView.findViewById(R.id.small_text);
                holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
    
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            if(listComment.get(position).isCheckbox()) {
                holder.checkBox.setChecked(true);
            }else {
                holder.checkBox.setChecked(false);
            }
    
            holder.body.setText(listComment.get(position).getMessageBody());
    
    
            holder.date.setText(listComment.get(position).getMessageDate());
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)  {
                    listComment.get(position).setIsCheckbox(isChecked);
                    if (isChecked){
                        deleteMemos.add(listComment.get(position));
                    }else{
                        deleteMemos.remove(listComment.get(position));
                    }
                }
    
            });
    
            return convertView;
        }