0

I have listview with toggle button .when i checked first toggle button the 5th toggle button get checked automatically. and when i checked 2nd toggle button 7th toggle button get checked .and when i unchecked that 5th toggle button it is toast the null value .

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                                                                         
     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);
        holder.tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);


        convertView.setTag(holder); //Add this
    }
    else
    {

       holder= (Holder) convertView.getTag();
    }

    holder.tvName.setText(user.name);
    holder.tvHome.setText(user.hometown);

    /** The clicked Item in the ListView */
    RelativeLayout rLayout = (RelativeLayout) convertView;

    /** Getting the toggle button corresponding to the clicked item */

    final ToggleButton tbt  = (ToggleButton) rLayout.getChildAt(2);

    tbt.setOnClickListener(new OnClickListener() {
        String homet;
        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
             if (tbt.isChecked()) {
                 //tbt.setChecked(true);
                 ViewGroup parent = (ViewGroup) v.getParent();
                 TextView tvName = (TextView) parent.findViewById(R.id.tvName);
                 homet=tvName.getText().toString();

                    Toast.makeText(getContext(),homet+"Blocked", Toast.LENGTH_SHORT).show();
                } else {
                    tbt.setChecked(false);
                    Toast.makeText(getContext(),homet+ "Unblocked", Toast.LENGTH_SHORT).show();
                }
        }
        });

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

        }
            //this holder class will be filled from the layout xml and attached to the row as a tag object


    private class Holder
    {
        TextView tvName;
        TextView tvHome;
        ToggleButton tgbtn,tg1;
    }
}

please help me...

and how to save the all toggle button states , so that i can used that saved state to preserve the state of toggle button when the app reopened.

Simran
  • 25
  • 6

1 Answers1

0

Your problem is happening because of ListView recycling mechanism. ListView recycles its children rows. For better understanding take a look here.
As to solving your problem, lot of people have already asked this question. The best blog I could find is this. You also might want to check this and this.

Community
  • 1
  • 1
Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • thanx for your reply. i understand the recycling mechanism,but didn't get any solution to avoid this situation , if i had a togglebutton and if i check it at position 0(let say item1 has also a togglebutton and ichecked it) so when i scroll down i will see item 8 togglebutton is already checked – Simran Feb 24 '15 at 06:22