简体   繁体   中英

How to check a checkbox if it is checked prevoiusly

I have implemented check box in a list, there is a names with check box. My problem is, I will select five names and I will display those names, after that if I come back to list, again those five names should be checked because I have already selected those names.

But now those are unchecked when I have revisited. How to achieve this.

My adapter code.

     holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
            holder.chkbox.setOnCheckedChangeListener(null);

            holder.chkbox.setChecked(checkedHolder[position]);
            Log.i(TAG, "Position " + checkedHolder[position]);

            if (checkedHolder[position])
                holder.chkbox.setChecked(true);
            else
                holder.chkbox.setChecked(false);

            holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {

                            checkedHolder[position] = isChecked;
                            Log.i(TAG, "Position " + checkedHolder[position]);
                        }
                    });

My activity code..

list.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                CheckBox cb = (CheckBox) view.findViewById(R.id.chkbox);

                cb.performClick();

                if (cb.isChecked()) {

                    for (int i = 0; i < adapter.getCount(); i++) {
                        if (adapter.checkedHolder[i]) {
                            // get all name values that checked by user
                            MemberName.clear();
                            MemberName.add(ItemsArr.get(i).getItemName().toString());
                            cb.setChecked(true);
                            Log.i(TAG, "Name " + MemberName);
                            // Name = ItemsArr.get(i).getItemName().toString();
                            //Log.i(TAG, "Name " + Name);
                        }
                    }
                } else if (!cb.isChecked()) {

                }    
            }    
        });

Unless you store the state somewhere, every time you go back to the list it will be treated like a new instance. As a result all boxes will go back to being unchecked.

When you go away from the list, save the checked values to some sort of persistent storage, like device storage, or pass the state of the boxes to the screen you're going to so that it can be passed back to the list when navigating back. Alternatively save them to a variable that has global scope and won't fall out of scope when moving screens.

When you go back to the list, see if there's a state to be used (either in the device storage, passed in as a parameter, or whatever means of persistence you use) then use it to re-check the appropriate boxes.

Try this. I have edit your code

 private static Map<Integer, Boolean> mapCheck = new HashMap<Integer, Boolean>();
     holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
        holder.chkbox.setOnCheckedChangeListener(null);

        holder.chkbox.setChecked(checkedHolder[position]);
        Log.i(TAG, "Position " + checkedHolder[position]);

        if (checkedHolder[position])
            holder.chkbox.setChecked(true);
        else
            holder.chkbox.setChecked(false);

        holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
            mapCheck.put(position, true);
                        checkedHolder[position] = isChecked;
                        Log.i(TAG, "Position " + checkedHolder[position]);
                    }
                });

And this:

list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            CheckBox cb = (CheckBox) view.findViewById(R.id.chkbox);

            cb.performClick();

            if (cb.isChecked()) {
        /*
                for (int i = 0; i < adapter.getCount(); i++) {
                    if (adapter.checkedHolder[i]) {
                        // get all name values that checked by user
                        MemberName.clear();
                        MemberName.add(ItemsArr.get(i).getItemName().toString());
                        cb.setChecked(true);
                        Log.i(TAG, "Name " + MemberName);
                        // Name = ItemsArr.get(i).getItemName().toString();
                        //Log.i(TAG, "Name " + Name);
                    } */

          //SEARCH POSITION WHERE ARE TRUE
           for(Integer i : mapCheck.keySet())
            {
          if(mapCheck.get(i) == true)
           {
             cb.setChecked(true);
           }
          }
                }
            } else if (!cb.isChecked()) {

            }

        }

    });

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