1

I had a Listview with Toggle Button on each row.On Scrolling the Listview got refreshed, therefore the value Toggle Button also getchanged on each time it is scrolled?

I want to keep that Toggle button value either ON/OFF as it set by the user before scrolling?

Sumit Sharma
  • 1,847
  • 1
  • 22
  • 25

5 Answers5

1
On Scrolling the Listview got refreshed,

It is the defualt behaviour of the ListView. It actually not refreshed, ListView only have the visible(with some offset) Views in the memory. and destroy others.

I want to keep that Toggle button value

You need to keep track off the states of the toggle buttons. Like store in Local arrayList. And update the togglebutton state in getView method of your adapter.

Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
1

try this in first line of getView Function

    if (convertView != null) return convertView;
0

When you toggle your button, you should also refresh your adapter's data source. For e.g if you are using an arraylist to populate the adapter, on toggle on the button, you have to update your arraylist.

Preventing refresh of the list view can also be achieved if you prevent recycling of rows.

Kiran Kumar
  • 1,192
  • 8
  • 10
0

You should set your ListView to use setChoiceMode() (with CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE) and use a custom row layout that implements Checkable, this way the ListView will remember which rows have been toggled for you.

I detailed how to create a custom Checkable layout before in CheckedTextView checkmark in ListView row not showing up. (It's not that hard and this approach allows you to use any built-in adapter without having to extend it.)

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
0

Use following: This will take care of your listView.

View vi = convertView;

if (convertView == null) {
            vi = inflater.inflate(R.layout.layoutFile, null);
            holder = new ViewHolder();
            holder.layout1 = (LinearLayout) vi
                    .findViewById(R.id.layoutcourse);
            holder.text = (TextView) vi.findViewById(R.id.text);
            holder.status = (ImageView) vi.findViewById(R.id.img_status);
            holder.date = (TextView) vi.findViewById(R.id.date1);
            holder.toggleButton = (ImageView) vi
                    .findViewById(R.id.toggleButton);
            vi.setTag(holder);
        } else

            holder = (ViewHolder) vi.getTag();
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • Where should I add this code? Also please describe the variables you have used in this code. – Sumit Sharma Dec 05 '12 at 07:32
  • in your adapter in getView() method, this should be first line of code – Shrikant Ballal Dec 05 '12 at 07:35
  • Inside this method: public View getView(final int position, View convertView, ViewGroup parent) { } – Shrikant Ballal Dec 05 '12 at 07:36
  • in getView() we have three agruments position,view & viewGroup. I can get the postion of the toggle button, view is for status than what is view Group – Sumit Sharma Dec 05 '12 at 08:11
  • @SumitSharma This is an good example of how to use a ViewHolder, but it does not address restoring the state of a ToggleButton... – Sam Dec 05 '12 at 08:19
  • When listView is scrolled, if view is already created, the view does not refresh itself if above method is used and if, the view is not refreshed, the toggle buttons' state will not change – Shrikant Ballal Dec 05 '12 at 08:31
  • That's a really bad answer: Way to broad and really not helpful because no explanation which has anything to say about the toggle button problem. – Bondax Oct 13 '15 at 08:14