Joseph
Joseph

Reputation: 99

ToggleButton in ListView, value changes automatically on scrolling

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;
}

Upvotes: 7

Views: 2501

Answers (4)

Beyaz
Beyaz

Reputation: 138

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.

Upvotes: 0

Chirag Patel
Chirag Patel

Reputation: 2320

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;
            }

Upvotes: 13

sastraxi
sastraxi

Reputation: 1338

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.

Upvotes: 0

Carl-Emil Kjellstrand
Carl-Emil Kjellstrand

Reputation: 1243

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.

Upvotes: 0

Related Questions