-2

I have a listview with a textview and switch.The listview is inside a settings page fragment.while clicking swiches,some settings gets changed(ON/OFF).Its works fine for me.Consider a situation,if i changed the third switch state from ON to OFF and i scroll the listview.After the third list element move inside the view and i return back the list to its original visible position,the third switch become in ON state.However,the third setting become in OFF state programatically,but it shown as ON.if i changed the fragment containing listview with another one(Home) and i return back to settings screen,then i can see the third switch is in OFF state.That means the list view item returns back to its loading state if it disappear while scrolling and return back.

rmanalo
  • 342
  • 3
  • 23
kannan
  • 317
  • 1
  • 4
  • 12

1 Answers1

1

The ListView elements are viewed by your getView() function in your adapter and they are re-generated when they appear in the visible part of the screen. Your switch may be programmatically set to OFF, yes, but when it appears on the screen again, getView() function works again and it appears as ON. So what you have to do is, in your getView(), you can check it's status whether it is ON or OFF, and then display it accordingly by using switch's own method.

Here is an example. The position parameter in getView() holds the index in your list. You can create a list of boolean values that hold the status of your switches. Let isToggled be the list, and switchBox be your switch button. Then you can add following in your getView() method;

if( isToggled(position) ){              
    switchBox.setChecked(true);
}
Faruk Yazici
  • 2,344
  • 18
  • 38
  • I approve this answer. Generally, object instances of elements of a ListView are re-used for performance reasons, so we have to set all parameters we want to influence inside the getView(..) function of the adapter. – schubakah Sep 01 '14 at 07:36