how to get the index and string of the selected item in listview in android
i have a listview like this
companyname symbol
samsung sam
onida ond
tata ta
. .
. .
. .
so i want to get the index 开发者_JS百科of the selected item and also string of the selected item(i.e.,sam(or)ond(or)ta) .So please tell me how to get the index of the selected item and string also .
Thank you in advance
use this way
listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView, int position, long id)
{
String text = ((TextView)childView.getText()).toString();
//The above text variable has the text value of selected item
// position will reflect the index of selected item
}
public void onNothingSelected(AdapterView parentView)
{
}
});
I would prefer implementing the activity as a ListActivity and override the onListItemClick method:
@override
protected void onListItemClick(ListView l, View v, int position, long id)
this gives you the position of the item clicked (I hope this is what you call index).
精彩评论