Search code examples
androidlistviewstatetogglebutton

How to preserve the state of toggle button in listview in android


i have listview with toggle button.i face a problem when i scroll down the toggle button the state of toggle button changed,and also when i click on first toggleButton the fourth toggle button also checked. i also want to save the all toggle button states and the text on that particular row in arraylist. I also want to preserve toggle button state when app is closed .

i tries to use holder but its not work.

the below is my code

    public class CustomUsersAdapter extends ArrayAdapter<User> {
        public CustomUsersAdapter(Context context, ArrayList<User> users) {
            super(context, 0, users);
        }

   @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
    //Get an instance of our cell holder                                                                         
        final Holder holder;

        holder = new Holder();

      // Get the data item for this position
      User user = getItem(position);    
         // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
       convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
    }
    // Lookup view for data population
    holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
    holder.tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
    //final ToggleButton tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);
    // Populate the data into the template view using the data object
    holder.tvName.setText(user.name);
    holder.tvHome.setText(user.hometown);
  //  holder.tgbtn.setTag(position);
    /** The clicked Item in the ListView */
   RelativeLayout rLayout = (RelativeLayout) convertView;

    /** Getting the toggle button corresponding to the clicked item */
 holder.tgbtn = (ToggleButton) rLayout.getChildAt(2);


  holder.tgbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             if (holder.tgbtn.isChecked()) {
                 holder.tgbtn.setChecked(true);
                 String homet=holder.tvHome.getText().toString();
                    Toast.makeText(getContext(),homet+"Blocked", Toast.LENGTH_SHORT).show();
                } else {
                    holder.tgbtn.setChecked(false);
                    Toast.makeText(getContext(), "Unblocked", Toast.LENGTH_SHORT).show();
                }
        }
        });


     // Return the completed view to render on screen
      return convertView;
        }


   private class Holder{

   TextView tvName;
   TextView tvHome;

   ToggleButton tgbtn,tg1;

    }
    }

Solution

  • when i scroll down the toggle button the state of toggle button changed

    Because currently getView method not implemented using ViewHolder Pattern in right way.

    See following tutorial to implement getView method using ViewHolder :

    Performance Tips for Android’s ListView

    when i click on first toggleButton the fourth toggle button also checked

    Because holder.tgbtn object of ToggleButton is used for check state of ToggeleButton on onClick. holder.tgbtn hold reference which is assigned during last call of getView method.

    Use v for getting text from TextView according to ToggelButton:

    @Override
      public void onClick(View v) {
       // TODO Auto-generated method stub
       ViewGroup parent = (ViewGroup) v.getParent();
       TextView tvName = (TextView) parent.findViewById(R.id.tvName);
       //   Use  tvName to get TextView text 
      }