从textview中检索自定义列表视图的文本

时间:2013-08-09 02:01:27

标签: java android android-listview

我有以下代码,如果长按列表视图,则会显示一个菜单:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    if (v.getId()==R.id.lvFiles) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.setHeaderTitle("What would you like to do?");
        String[] menuItems = getResources().getStringArray(R.array.menuSelect); 
        for (int i = 0; i<menuItems.length; i++) {
            menu.add(Menu.NONE, i, i, menuItems[i]);
        }
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    int menuItemIndex = item.getItemId();
    String[] menuItems = getResources().getStringArray(R.array.menuSelect);
    String menuItemName = menuItems[menuItemIndex];

    TextView ck = (TextView) mFrame4.findViewById(R.id.txtTitle);

    Toast.makeText(getActivity(), String.format("Selected %s and %s", menuItemName, ck.getText().toString()), 2000).show();
    return true;
}

我的自定义列表视图调用另一个具有textview的布局。如何从列表视图中显示所选列表的文本值?

Toast.makeText(getActivity(), String.format("Selected %s and %s", menuItemName, DISPLAY THE TEXT OF THE SELECTED TEXTVIEW, 2000).show();

1 个答案:

答案 0 :(得分:4)

您可以使用以下代码获取TextView的文字:

String txt = ((TextView) info.targetView.findViewById(R.id.txtTitle)).getText().toString();