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?