1

我试图理解用于创建上下文菜单的 android API 指南,但它们确实没有对应该如何使用 AdapterContextMenuInfo 给出任何解释。

我的代码:

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsAdapterView.AdapterContextMenuInfo;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
...

public class MyActivity extends SherlockActivity {}
    ...
    class MyCursorAdapter extends CursorAdapter {
        ...
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            View rowView = inflater.inflate(R.layout.list_row, parent, false);
            registerForContextMenu(rowView);
            return rowView;
        }
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        android.view.MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context, menu);
    }

    @Override
    public boolean onContextItemSelected(android.view.MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        Log.d(MyApp.LOG_TAG, "info is null: "+ (info == null)); // info is null: true
        ...
    }
}

根据我对上下文菜单应该如何工作的模糊理解,我希望 info 包含 db row id 或至少包含我可以从中检索 id 作为标签的 targetView 。

请让我知道我哪里出错了,或者我如何检测上下文菜单显示的 ListView 项。

4

1 回答 1

0

android - 为什么 item.getMenuInfo 为空

提问者没有将其标记为已接受的答案,所以我不知道它是否解决了他的问题,但看起来你正在做和他一样的事情。

将菜单注册为上下文菜单而不是行项。

于 2013-07-13T22:28:57.727 回答