简体   繁体   中英

Android ListView with checkboxes loose checked items when scrolling

I'm facing with one problem, it's about disappearing checked items in listview when I'm scrolling the list and I don't know what to do.

Here is my code of custom adapter:

public class SPCKontrola extends Activity {
ArrayList<Integer> yourSelectedItems= new ArrayList<Integer>();

@Override
        public void onResume() {
            super.onResume();

            yourSelectedItems.clear();

        }
}

public class SPCMjereAdapter extends BaseAdapter 
    {
        private Context context;

        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

        public int getCount() {
            return MyArrList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") );

            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));

            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
            Chk.setTag(MyArrList.get(position).get("RbMjere"));



Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            yourSelectedItems.add(position);
                        }else{
                            if(yourSelectedItems.contains(position)){                                               
                                yourSelectedItems.remove((position));                                   
                            }
                        }
                    }
                    });


            return convertView;

        }
}

What should I change or add in code? I have found some examples but I can't reuse non of them and implement in my code :/

for this you have to maintain an array variable yourSelectedItems[] for storing the view positions that you have checked..and put a condition like this in your getView() method..

if(yourSelectedItems.contains((position))){
    check.setChecked(true);
}else{
    check.setChecked(false);
}

you can manage you array by writing this method in your getView() ..

 check.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            yourSelectedItems.add(position);
        }else{
            if(yourSelectedItems.contains(position)){                                               
                yourSelectedItems.remove((position));                                   
            }
        }
    });

尝试使用带有复选框的listview这个示例: http : //lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html

To avoid the problems with CheckBoxes in a ListView you can take a Boolean array initialized false in the beginning and then making true the corresponding position in the array where checkbox is checked in the ListView. This will not cause a trouble in checked state of checkboxes when you move forward and backward in your application or scroll the ListView.You can check sample code here:

Android checkbox multiselected issue

This happened to me and i solve this wtih,

1.Create a global boolean arraylist and fill it with MyArrList.size() on constructor.

2.When you changed checkbox value set arratlist with views index.

3.Change this:

 CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
 Chk.setTag(MyArrList.get(position).get("RbMjere"));

with this:

 CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
 Chk.setTag(MyArrList.get(position).get("RbMjere"));
 Chk.setChecked(commentTexts.get(position));

change your getview() with this one...

     public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") );

            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));

            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);


             if(yourSelectedItems.contains((position))){
                Chk.setChecked(true);
             }else{
                Chk.setChecked(false);
             }

            Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            yourSelectedItems.add(position);
                        }else{
                            if(yourSelectedItems.contains(position)){                                               
                                yourSelectedItems.remove((position));                                   
                            }
                        }
                    }
                    });


            return convertView;

        }

如果您在listView.onItemSelectedListner中使用了getView,您将面临这个问题。listView不会将每个对象都放在不同的视图中,这就是为什么当您调用getView(2)时它将在屏幕上返回列表的第二个对象的视图而不是列表第二个对象的视图 ...您必须使用类中的一些布尔值或复选框自己实现它

I think I get something... Instead of Chk.setOnCheckedChangeListener() I've changed to Chk.setOnClickListener(new OnClickListener()

public class SPCMjereAdapter extends BaseAdapter 
    {
        private Context context;


        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

        public int getCount() {
            return MyArrList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") );

            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));

            final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);


            if(yourSelectedItems.contains((position))){
                Chk.setChecked(true);
             }else{
                Chk.setChecked(false);
             }

            Chk.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (Chk.isChecked())
                    {
                         yourSelectedItems.add(position);
                    } else
                    {
                        if(yourSelectedItems.contains(position)){                                               
                            yourSelectedItems.remove((position));                                   
                        }
                    }

                }
            });


            return convertView;
        }

 }

Now it works fine when scrolling up/down. But there is another problem and i'm little bit confused. When scrolling and randomly check/uncheck items program crashes and these errors I get:

FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.remove(ArrayList.java:399)
at com.dem.spckontrola.SPCKontrola$SPCMjereAdapter$1.onClick(SPCKontrola.java:830)
at android.view.View.performClick(View.java:4084)
at android.widget.CompoundButton.performClick(CompoundButton.java:100)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method

)

Ok. I solved this by adding yourSelectedItems.remove(yourSelectedItems.indexOf(position));

So now the correct solution is:

 public class SPCMjereAdapter extends BaseAdapter 
    {
        private Context context;


        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

        public int getCount() {
            return MyArrList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") );

            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));

            final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);


            if(yourSelectedItems.contains((position))){
                Chk.setChecked(true);
             }else{
                Chk.setChecked(false);
             }

            Chk.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (Chk.isChecked())
                    {
                         yourSelectedItems.add(position);
                    } else
                    {
                        if(yourSelectedItems.contains(position)){                                               
                            yourSelectedItems.remove(yourSelectedItems.indexOf(position));                                   
                        }
                    }

                }
            });


            return convertView;
        }

 }

But now I'm facing with new problem: How to get list of all checked and unchecked items where some checkboxes (items) are not visible due to many items in listview which are visible only when scrolling down?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM