I have made a listview. The plan is that when you select an item, it should appear selected (background color changes) and when you select another one, the one that was selected previously is normal again. Is there a way to do this? I've been trying a bunch of things and nothing works...
This is my code so far...
/*Listview testing*/
final ListView listview = (ListView) findViewById(R.id.listView1);
String[] values = new String[] {
"Case White",
"Operation Weser-Exercise",
"Case Yellow",
"April War",
"Operation Barbarossa",
"D-day" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//This doesn't work:
//listview.findViewById((int) selid).setBackgroundColor(Color.TRANSPARENT);
view.setSelected(true);
view.setBackgroundColor(Color.LTGRAY);
Context context = getApplicationContext();
CharSequence text = "id: " + id;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
selid = id;
}
});
Marking one of them works, but then removing the selection is where I'm stuck. Any suggestions?
EDIT: what I'm looking for is for it to stay selected until I select another item