我有一個列表視圖的應用程序。列表視圖正常工作。當我想讓列表中的某些行標記時,問題就開始了。如果我按下它,我可以標記一行。但是,似乎沒有找到一種方法來標記任何初始化行。如何在ListView中標記視圖?
這是我的代碼:
listViewOfBluetooth = getListView();
setInitialEnabledDevices();
listViewOfBluetooth.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String chosenBluetoothDevice = (String) ((TextView) view).getText();
BluetoothEnableOrDisable(view, chosenBluetoothDevice);
Toast.makeText(getApplicationContext(), chosenBluetoothDevice, Toast.LENGTH_SHORT).show();
editor.putString("bluetooth_name_from_list1", chosenBluetoothDevice);
editor.putBoolean("have_the_cars_bluetooth", true);
editor.commit();
Intent intent = new Intent(List.this, ParkOGuardActivity.class);
startActivity(intent);
}
});
}
public static void setInitialEnabledDevices(){
int length = listViewOfBluetooth.getChildCount();
View view = null;
String first = prefs.getString("bluetooth_name_from_list0", "");
String second = prefs.getString("bluetooth_name_from_list1", "");
String third = prefs.getString("bluetooth_name_from_list2", "");
for(int i = 0; i < length; i++){
view = listViewOfBluetooth.getChildAt(i);
if(view.equals(first) || view.equals(second) || view.equals(third)) {
view.setBackgroundColor(Color.GRAY);
}
}
}
我怎樣才能解決這個問題?
謝謝!
你使用什麼適配器? – Shaiful
我使用listadapter:setListAdapter(new ArrayAdapter(this,R.layout.list,arrayOfBluetoothDevicesNames)); –
roiberg
爲什麼不創建一個擴展ArrayAdapter的Adapter? 並重寫getView()方法。 – IvoryCirrus