0

我知道通过执行以下操作长按时需要在上下文菜单中找到一行:

  AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        int position = info.position;

onCreateContextMenu

我怎样才能在这里实现它?我似乎只抓住了这个设置中的前两个。

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

    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.comments_context, menu);
    menu.setHeaderTitle("Available Actions");

    MenuItem Edit = menu.findItem(R.id.editComment);
    MenuItem Delete = menu.findItem(R.id.deleteComment);

    ReviewUser = ((TextView) v
            .findViewById(R.id.labelReviewCommentUser)).getText()
            .toString();

    ReviewComment = ((TextView) v
            .findViewById(R.id.labelReviewComment)).getText()
            .toString();



}

我希望ReviewUserReviewComment根据用户单击的列表行被选中。

4

1 回答 1

1

我怎样才能在这里实现它?我似乎只抓住了这个设置中的前两个。

v表示View为其构建上下文菜单的对象,在这种情况下,ListView不是您执行操作的行视图。你可能想要这样的东西:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.comments_context, menu);
    menu.setHeaderTitle("Available Actions");

    MenuItem Edit = menu.findItem(R.id.editComment);
    MenuItem Delete = menu.findItem(R.id.deleteComment);
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    int position = info.position;
    ListView lv = (ListView) v;
    int firstVisible = lv.getFirstVisiblePosition();
    View rowView = lv.getChildAt(position - firstVisible);
    ReviewUser = ((TextView) rowView.findViewById(R.id.labelReviewCommentUser))
            .getText().toString();
    ReviewComment = ((TextView) rowView.findViewById(R.id.labelReviewComment))
            .getText().toString();
}
于 2012-12-14T18:15:11.910 回答