Vaibhav
Vaibhav

Reputation: 21

Toggle button in a list view loose their state when scrolled of screen in Android

I have a list view in which each data is fetched from List adapter. Each item in list view contains a text view and toggle button. But there occurs a problem when i scroll the list. the checked state of toggle buttons change i.e if i selected a toggle button at index 4, then after scrolling i find that any random button except fourth is selected.

Upvotes: 2

Views: 3692

Answers (3)

Nikhil
Nikhil

Reputation: 80

I know this is a really old thread , but the answers I found to the question were not really great. It took me some time to get a workable solution from the two answers. I just wanted to share the working code, if any other noob is trying to figure out how persist the state of toggle button while scrolling.

private class MyListAdapter extends ArrayAdapter<Product> {
     boolean [] switchState= new boolean[100] ;
    public MyListAdapter() {
        super(ProductSelectActivity.this, R.layout.item_view, myProducts);
    }

public View getView( int position, View convertView, ViewGroup parent) {
        // Make sure we have a view to work with (may have been given null)

        ViewHolder holder;

        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
            View row= itemView;
            holder= new ViewHolder(row);
            itemView.setTag(holder);
            holder.switchButton  = (ToggleButton) itemView.findViewById(R.id.favorite_button); 

        }
        else
        {
            holder = (ViewHolder) itemView.getTag();

        }


        holder.switchButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                int index =  (Integer) v.getTag();

                if(((ToggleButton) v).isChecked()) {
                    switchState[index] = true; 
                    ((ToggleButton) v).setBackgroundResource(android.R.drawable.btn_star_big_on);

                }
                else {
                    ((ToggleButton) v).setBackgroundResource(android.R.drawable.btn_star_big_off);
                    switchState[index] =false; 
                }

            }
        });
        if(switchState[position])   
            holder.switchButton.setBackgroundResource(android.R.drawable.btn_star_big_on);

        else
            holder.switchButton.setBackgroundResource(android.R.drawable.btn_star_big_off);

    //holder.categoryName.setText(categories[position]);
    holder.switchButton.setTag(Integer.valueOf(position));

ViewHolder.class

public class ViewHolder {
ToggleButton switchButton=null;

  ViewHolder(View base) {
    this.switchButton=(ToggleButton)base.findViewById(R.id.favorite_button);
  }

}

This code compiles.

Upvotes: 0

Ragesh Gopal
Ragesh Gopal

Reputation: 79

if u still doesn't get any solution pls try this getview method.. // switchState[] is a boolean array .

public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder = new ViewHolder();
         // mSwitchButton = (ToggleButton) findViewById(R.id.switchButton);
        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.settings_item_cell, parent, false);
            holder.categoryName =
                    (TextView) convertView.findViewById(R.id.categoryType);
            convertView.setTag(holder);
            holder.switchButton  = (ToggleButton) convertView.findViewById(R.id.switchButton); 
        }
        else {
            holder = (ViewHolder) convertView.getTag();


        }

            holder.switchButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    int index =  (Integer) v.getTag();
                    Log.v("tag of switch============",""+index);
                    if(((ToggleButton) v).isChecked()) {
                        switchState[index] = true; 
                        ((ToggleButton) v).setButtonDrawable(R.drawable.item_selected);

                    }
                    else {
                        ((ToggleButton) v).setButtonDrawable(R.drawable.item_deselected);
                        switchState[index] =false; 
                    }
                    isToggleButtonClicked = true;

                }
            });  
            if(switchState[position])   
                holder.switchButton.setButtonDrawable(R.drawable.item_selected);
            else
                holder.switchButton.setButtonDrawable(R.drawable.item_deselected);

        holder.categoryName.setText(categories[position]);
        holder.switchButton.setTag(new Integer(position));

        return convertView;

    }

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006859

Since rows get recycled, you need to maintain your own state, saving and restoring your ToggleButton status as it is modified. Here is a sample project demonstrating using a RatingBar in rows; the same basic process should hold for a ToggleButton.

Upvotes: 2

Related Questions