2 for 2012 for_2012 于 2013.08.09 13:59 提问

检索自定义listView中的文本

下面的代码实现功能是长按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;
}

自定义listView调用了另一个布局,带有一个textView,如何显示选中的文本值?

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

1个回答

Kill_it
Kill_it   2013.08.09 16:48

获取TextView的文本:

String txt = ((TextView) info.targetView.findViewById(R.id.txtTitle)).getText().toString();
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!