我有一個複選框傳感器的簡單列表,Android的多選列表中選擇上滾動隨機元素
如果我選擇任何傳感器,然後上下滾動列表,選中的傳感器得到改變,有時我會看到更多的傳感器檢查,有時沒有傳感器將被檢查。所有這些都是通過上下滾動列表完成的。
這是一個仍然有SAM問題的最低限度的程序:
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);
}
});
}
}
這裏是佈局:
<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>
任何想法我怎麼能得到這個東西正常工作?
退房這個答案,它可以幫助:http://stackoverflow.com/a/20171191/919216 –