how to get listitem text in onItemClick listener?
开发者_C百科I have created a list view and i have added some item, can anybody help me to get the item text in on Item Click listener?
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView <? > parent, View view,
int position, long id) {
Object o = parent.getItemAtPosition(position);
String keyword = o.toString();
Log.v("value ", "result is " + keyword);
}
});
I have tried the above code but it does not work...
I assume you have TextView
on the item.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView <? > parent, View view,
int position, long id) {
TextView txt = (TextView) parent.getChildAt(position - lv.firstVisiblePosition()).findViewById(R.id.mylistviewtextview);
String keyword = txt.getText().toString();
Log.v("value ", "result is " + keyword);
}
});
or you can retrieve the text from the ArrayList
you feed the ListView
First of all, the correct event to intercept for selection is onItemSelected
because a selection might not have been made in the onClick handler. And then the question has been well treated here on stackoverflow.
精彩评论