Android ListView and OnClickListener: How to get the selected item

最后都变了- 提交于 2019-12-08 23:50:24

问题


I have a listView vith some items.

I would like to get from my onClickListener the name (String) of the selected item.

I know how to get the selected position but how to find the String of this element?

Here is my on click listener:

journalNames.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
    {

    }});

My listView is populated with some query from the database.

Thank you.


回答1:


What about,

journalNames.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
    {
      String selectedFromList = (journalNames.getItemAtPosition(position).getString());
    }});



回答2:


YOu can find it either on view or on parent. In eclipse just type view. and see what methods you get after you type .(dot). I think this is the best.

parent.getAdapter().getItem(position);


来源:https://stackoverflow.com/questions/9427797/android-listview-and-onclicklistener-how-to-get-the-selected-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!