1

I have a simple list of sensors with checkboxes,

If I select any sensor, and then scroll up and down the list, the checked sensors get changed, sometimes I'll see more sensors checked, sometimes none of the sensors will be checked. All that done just by scrolling the list up and down.

This is the bare minimum program that still has the sam issues:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> sensors = new ArrayList<String>();
        SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        List<Sensor> mSensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);

        for (Sensor s : mSensorList) {
            sensors.add(s.getName());
        }

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, sensors);

        ListView mSensorListView = ((ListView) findViewById(R.id.sensorListView));
        mSensorListView.setAdapter(arrayAdapter);

        mSensorListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckedTextView check = (CheckedTextView) view;
                check.setChecked(!check.isChecked());

                // this only logs when I actually click on a sensor. now when I scroll.
                Log.v("SENSOR CLICKED: ", check.getText() + "  " + position);
            }
        });

    }

}

and here is the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/sensorListView"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_marginTop="20dp"
        android:layout_height="300dp"
        android:background="#dadde0">

    </ListView>

</RelativeLayout>

any idea how I can get this thing to work normally?

zidarsk8
  • 3,088
  • 4
  • 23
  • 30

1 Answers1

2

You have to create a custom adapter view holder class with your layout view objects along with the position and reuse it.

follow this example : http://sunil-android.blogspot.in/2013/04/android-listview-checkbox-example.html

AndroidDev
  • 888
  • 3
  • 13
  • 27
  • 1
    To explain why: When you scroll the list it recycles the views used to display the actual items. Because it does not know that you have changed the view by checking a button it does not know that the recycled view needs to be cleaned up. Your custom adapter can do the clean-up. – Dale Wilson Oct 29 '13 at 21:27
  • Thank you @DaleWilson , But it's weird that it's that hard to create a simple checkbox list view. Also the example in the answer has a lot of bugs and syntax errors in itself. – zidarsk8 Oct 29 '13 at 22:09