I need to highlight a row in a ListView that was selected (to show the user what he chose), so, it’s not the one that is going to be chosen, it’s the one he chose before.
I already have the location by:
ListView.setSelection(position);
And now what I want is to select this specific row and to highlight it.
The code of the onCreate() in the activity that contains the ListView:
public class CountryView extends Activity
{
protected static final String LOG_TAG = null;
/** Called when the activity is first created. */
String[] lv_arr = {};
ListAdapter adapter;
TextView t;
private ListView lvUsers;
private ArrayList<Coun> mListUsers;
String responce=null;
public int d;
int selectedListItem = -1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.country);
Intent data =getIntent();
mListUsers = getCoun();
lvUsers = (ListView) findViewById(R.id.counlistView);
lvUsers.setAdapter(new ListAdapter(this, R.id.counlistView, mListUsers));
selectedListItem=data.getExtras().getInt("PositionInList");
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setOnItemClickListener(new OnItemClickListener()
{
int positionItem;
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Intent pongIntent = new Intent(getApplicationContext(),Trav.class);
int counId=mListUsers.get(position).id;
pongIntent.putExtra("response",mListUsers.get(position).p);
pongIntent.putExtra("responseCounID",counId);
//Put the position of the choose list inside extra
positionItem=position;
pongIntent.putExtra("PositionInListSet",positionItem);
setResult(Activity.RESULT_OK,pongIntent);
Log.i("CounID *******************************"," "+counId);
finish();
}
});
}
}
Since by default
ListViewsare set to a selection mode ofNONE, in touch mode thesetSelectionmethod won’t have visual effect.For keeping the previous selection / visually display an explicit selection, first you must set your listview’s choice mode appropriately:
It’s useful to read the API Docs of these methods:
In case this is not enough (say you’d like to always show the last selection differently beside the current selection), you should store your last selected item (a data which populates the
ListAdapter) aslastSelectedItem, and in your adapter’sgetViewmethod assign a different background resource to the renderer if it equals thislastSelectedItem.If your last selection wouldn’t refresh on selection change, you should explicitly call the
notifyDataSetChangedmethod on your adapter instance.Update
Since your activity containing the
ListViewis a child of an activity which waits for this one’s result (based on thesetResult(Activity.RESULT_OK,pongIntent);part), the initial idea is correct, the last position should be passed through the intent when starting the activity:The
ListView.CHOICE_MODE_SINGLEsolution would work if you remain in the same activity, but you are finishing it on every itemClick (selection change), that’s why the extra data should be passed to the startingIntent.You can also set the previously selected item’s background from your adapter -as mentioned above-, overriding its
getViewmethod: