ToggleButton in ListView, value changes automatically on scrolling
Asked Answered
S

4

7

I have a ListView with some items. I have toggleButton in each row of the ListView. Assume none of the toggleButtons are selected. The scroll works fine. But when I check the toogleButton, and then scroll my listView, when the selected toggleButton's row moves up, the last toggleButton(which is unchecked) gets checked automatically. And this pattern goes on. I think it has something to do with the reusing the rows of the listItems. I have added the adapter class below, where the list item loads

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View rowview = convertView;
    if (null == rowview) {

        rowview = inflator.inflate(R.layout.groupsettinglistitem, null);            
        SettingsGroupListItem viewholder=new SettingsGroupListItem();
        viewholder.gpname=(TextView) rowview.findViewById(R.id.textView1);
        viewholder.status=(ToggleButton) rowview.findViewById(R.id.ToggleButton1);
        viewholder.status.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
            Toast.makeText(v.getContext(), "click", Toast.LENGTH_SHORT);    
            }
        });

        rowview.setTag(viewholder);

    }
    SettingsGroupListItem holder=(SettingsGroupListItem) rowview.getTag();
    holder.gpname.setText(items[position].getGpname().getText().toString());
    rowview.setTag(holder);
    return rowview;
}
Silden answered 25/3, 2012 at 18:15 Comment(0)
G
13

This two Method add in your BaseAdapter class.

          @Override
            public int getViewTypeCount() {                 
                          //Count=Size of ArrayList.
                return Count;
            }

            @Override
            public int getItemViewType(int position) {

                return position;
            }
Geri answered 26/3, 2012 at 9:43 Comment(4)
This solution worked, could some one please explain the reason for this behavior.Bohlin
it forcely not reusing the viewsSeamount
after adding those when get loaded more items to the array it will get arrayindexoutofbound exception :(Musicology
that answer is only for listview that is not enabled scroll.If you use scroll this answer is wrongTeller
C
0

Your are correct, you will need to keep track of what state each button have outside the list element since they do get recycled. Creating a ArrayList for example and put the state for each button when clicked in the ArrayList, and in your getView you can look at ArrayList.get(possition) to determine if the buttons state should be up or down.

Checkpoint answered 25/3, 2012 at 18:39 Comment(0)
S
0

I believe--correct me if I'm wrong--that this is a duplicate of Force Listview not to reuse views (Checkbox).

You need to set the state of the Checkbox when you create it in getView to whatever it should be based on your data model.

Shoreline answered 25/3, 2012 at 18:40 Comment(0)
T
0

Important

Users "Chirag Patel" mentioned on answer that method

public int getViewTypeCount() and public int getItemViewType(int position) fix like Tooglebutton automaticly enable state check true on scrolling..that is big wrong .If you dont want automatic enable on scrool just do

toogleButton.setChecked(false);

on getView override method.

Teller answered 17/10, 2015 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.