How do I get the text set on a ListView item

偶尔善良 提交于 2019-12-12 05:37:19

问题


What I want to do I have several items in a ListView. When I longpress an item I have a contextmenu which pops up with several options. On of these options is "Share task". When the user selects "Share task" I want to have the Title/Text of the item they long-pressed to be retrieved and passed to a method.

What's I've managed to do so far I have managed to get the id and position of the item in the listView which the contextmenu was called up using the method show below.

@Override
public boolean onContextItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.menuShare :
        //Identify list item on which editing needs to be performed
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

        //METHOD TO SHARE TASK NEEDS TO GO HERE
        shareTask();
        return true;
    }
    return super.onContextItemSelected(item);
}

What I need help with I can't figure out how I can now get the text/title of the item on which the contextmenu was called. I intend to then pass this string to the shareTask() function to fire up a chooser. The title I have passed will then be used for example as the title of an email.

Thank you very much in advance!


回答1:


You should use the following code to get text of list item:

AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
//list item's text on which long-pressed is performed
String text = yourListView.getAdapter().getItem(info.position).toString();


来源:https://stackoverflow.com/questions/9762099/how-do-i-get-the-text-set-on-a-listview-item

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