Android从上下文菜单中的自定义列表视图中获取所选文本

时间:2014-08-15 09:37:49

标签: android listview android-listview

如果您长按项目,我有一个带有上下文菜单的自定义列表视图。我想做的是从listview项中获取所选文本并将其传递给上下文菜单。我怎样才能做到这一点?

这是我到目前为止所尝试的:

private String selectedWord = null;
private long selectedWordId = -1L;

private void initControls(View rootView) {
   lv = (ListView) rootView.findViewById(R.id.listView1);

   registerForContextMenu(lv);

}

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {

    AdapterView.AdapterContextMenuInfo info =
            (AdapterView.AdapterContextMenuInfo) menuInfo;
    selectedWord = ((TextView) info.targetView).getText().toString();
    selectedWordId = info.id;

    /*
      if (view.getId()==R.id.listView1) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.setHeaderTitle("Nothing");
      */ 

        String[] menuItems = getResources().getStringArray(R.array.context_menu);
        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.context_menu);
      String menuItemName = menuItems[menuItemIndex];

      // To get the id of selected item from custom listview
      long id = adapter.getItemId(info.position);


      Log.e("TESTS", "" + id);

      if(menuItemName.equals(getString(R.string.action_view))) {

          /*Intent i = new Intent(getActivity(), OrdinanceViewActivity.class);
          i.putExtra("Ordinance ID", ordinance_id);
          Log.e("I'm Here!", "" + ordinance_id);
          startActivity(i);  */

      } else {
         // adapter.remove(adapter.getItem(info.position));
      }


      return true;

}

class CustomAdapter extends CursorAdapter  {

    LayoutInflater inflater;
    @SuppressWarnings("deprecation")
    public CustomAdapter(Context context, Cursor c) {
        super(context, c);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {


        tvOrdinance = (TextView) view.findViewById(R.id.tvOrdinance);
        tvID = (TextView) view.findViewById(R.id.tvID);
        tvCategory = (TextView) view.findViewById(R.id.tvCategory);

        tvID.setText(cursor.getString(
                cursor.getColumnIndex(DBHelper.KEY_ID)));
        tvCategory.setText(cursor.getString(
                cursor.getColumnIndex(DBHelper.KEY_CATEGORY_NAME)));
        tvOrdinance.setText(cursor.getString(
                cursor.getColumnIndex(DBHelper.KEY_ORDINANCE_TITLE)));

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(R.layout.list_bookmarks_item, parent, false);
     }

}

但是我在这一行得到了一个ClassCastException:

selectedWord = ((TextView) info.targetView).getText().toString();

我在这做错了什么?任何帮助,将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

我想,你必须更换

selectedWord = ((TextView) info.targetView).getText().toString();

selectedWord = ((TextView)info.targetView.findViewById(R.id.YOUR_TEXTVIEW_ID)).getText().toString();