[SOLVED] I have made listview with checkbox but while scrolling listview more checkbox is select randomly and it does not hold their position

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under CC BY-SA 2.5.CC BY-SA 3.0.CC BY-SA 4.0.

Issue

I have made ListView with CheckBox but while scrolling listview more CheckBox is select randomly and it does not hold their position. Please help me. thanks in advance

public override View GetView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = context.LayoutInflater.Inflate(Resource.Layout.row1, parent, false);
    }
    Data item = this[position];
    view.FindViewById<TextView>(Resource.Id.title).Text = item.nameview;
    view.FindViewById<TextView>(Resource.Id.reporter).Text = item.EmailIdview;
    checkbox = view.FindViewById<CheckBox>(Resource.Id.checkbox);          
    checkbox.Tag = item.nameview;
    checkbox.SetOnCheckedChangeListener(new CheckedChangeListener(context, list, position));
    return view;
}

public class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener {

    private Activity context;
    private List<Data> list;
    private  int mPosition;

    public CheckedChangeListener(Activity context, List<Data> list, int mPosition) {
        this.context = context;
        this.list = list;
        this.mPosition = mPosition;
    }

    public void OnCheckedChanged(CompoundButton buttonView, bool isChecked) {
        Group gr = new Group();                
        string name = buttonView.Tag.ToString();
        if (isChecked) {
            gr.checkboxSelected(name, isChecked);//list.ElementAt(mPosition)                    
        } else {                
            gr.setPosition(mPosition);                    
        }
    }           
}

Solution

you can use model class to hold the checked status of checkbox. As i can see yo have a model class called Data

you can add isChecked in Data model class like this,

public class Data {

    String name;
    boolean isChecked;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }
}

Now you have some sort of adapter class : use view holder pattern

you can set tag for checkbox. I can give yo reference example for this follow the same you will get desired result.

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.List;

public class DataAdapter extends BaseAdapter {


    public Activity context;

    public List<Data> list;

    public DataAdapter(Activity context, List<Data> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return list.size();
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(R.layout.list_row, viewGroup, false);
            holder = new Holder();
            holder.textView = (TextView) view.findViewById(R.id.textView);
            holder.checkBox = (CheckBox) view.findViewById(R.id.checkbox);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.checkBox.setOnCheckedChangeListener(new CheckedChangeListener());
        holder.checkBox.setTag(position);//important line
        holder.textView.setText(list.get(position).getName());
        holder.checkBox.setChecked(list.get(position).isChecked());
        return view;
    }

    private class CheckedChangeListener implements CompoundButton.OnCheckedChangeListener {

        public CheckedChangeListener() {
        }
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            int position=(int) compoundButton.getTag();//important line
            list.get(position).setChecked(b);//important line
        }
    }

    public static class Holder {
        TextView textView;
        CheckBox checkBox;
    }
}

Happy coding ;)

Answered By – santosh kumar

people found this article helpful. What about you?

Exit mobile version