I have a ListView with two elements. I would like to execute different code based on the item that has been selected. Until now my code looks like this (but it is not working):
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
ListView listview = (ListView) findViewById(android.R.id.list);
View root = (View) listview.getParent();
if (position == 1) {
root.setBackgroundColor(Color.parseColor("#133b96"));
} else if (position == 2) {
root.setBackgroundColor(Color.parseColor("#bdbdbd"));
}
}
I want to change the background color, and the color should be based on the selected item.
What is the simplest way to do this?
Just look at your
protected void onListItemClick()methods for parametersView v. Which is a Selected list item’s View. And if you want to change background color of ListView then useListView l.something like,
So you have to remove additional code from it to get it to work.