如何根据ListActivity中长按的项目设置特定的上下文菜单?

时间:2010-12-04 17:28:12

标签: android listview contextmenu

我有一个列表活动,我选择手动添加第一个项目“添加新项目...”。

我已使用registerForContextMenu(getListView());直接在onCreate注册了整个列表视图的上下文菜单。

构建上下文菜单时,系统会调用onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)View v是listView,我无法找到方法来了解列表视图中的哪个项目被长按。

我可以创建一个xml布局,其中包含“添加新项目...”的布局,然后添加一个listview,它将由活动填充,并且会对上下文菜单做出反应,但我是确定没有任何xml布局可以解决这个问题。

我尝试使用registerForContextMenu在我的列表视图中注册每个视图,这有效,但列表视图不再响应触摸。

这是我的活动代码列表:

public class AMain extends ListActivity {
    private List<String> pregList;
    private List<Long> pregIds;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pregList = new ArrayList<String>();
        pregIds = new ArrayList<Long>();

        registerForContextMenu(getListView());
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        // TODO: hide the menu for the 1st item!!
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Logger.d("id = "+info.id);
        switch (item.getItemId()) {
        case R.id.menu_show:
            showPregnancy((int) info.id);
            return true;

        case R.id.menu_edit:
            editPregnancy((int) info.id);
            return true;

        case R.id.menu_delete:
            //TODO: do the deletion
            return true;

        default:
            return super.onContextItemSelected(item);
        }
    }

    protected void onStart() {
        super.onStart();

        clearPregList();
        loadPregList();
        getListView().setAdapter(new PregnancyListAdapter(this));
    }

    void clearPregList() {
        pregList.clear();
        pregIds.clear();
    }

    void loadPregList() {
        PregnanciesDbAdapter db = new PregnanciesDbAdapter(this);
        db.open();
        Cursor c = db.getPregnancies();

        if (c != null) {
            do {
                pregList.add(c.getString(c.getColumnIndex(PregnanciesDbAdapter.KEY_PREG_NOM)));
                pregIds.add(c.getLong(c.getColumnIndex(PregnanciesDbAdapter.KEY_PREG_ROWID)));
            } while (c.moveToNext());
            c.close();
        }

        db.close();
    }

    private class PregnancyListAdapter extends BaseAdapter {
        private Context context;

        public PregnancyListAdapter(Context ctx) {
            context = ctx;
        }

        @Override
        public int getCount() {
            return pregList.size()+1;
        }

        @Override
        public Object getItem(int position) {
            if (position == 0) { // add button
                return getString(R.string.addPreg);
            } else {
                return pregList.get(position-1);
            }
        }

        @Override
        public long getItemId(int position) {
            if (position == 0) {
                return -1;
            }
            return pregIds.get(position-1);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout itemLayout;

            itemLayout= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.homelist_item_pregnancy, parent, false);

            ImageView logo = (ImageView) itemLayout.findViewById(R.id.logo);
            TextView pregName = (TextView) itemLayout.findViewById(R.id.pregName);

            if (position == 0) {
                itemLayout.setFocusable(false);
                itemLayout.setFocusableInTouchMode(false);
                pregName.setText(getString(R.string.addPreg));
            } else {
                logo.setVisibility(View.INVISIBLE);
                pregName.setText(pregList.get(position-1));
            }

            itemLayout.setId(position);

            return itemLayout;
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if (position == 0) {
            startActivity(prepareIntent(AShowPregnancy.class, 0));
        } else {
            showPregnancy(position-1);
        }
    }

    void showPregnancy(int pregId) {
        startActivity(prepareIntent(AShowPregnancy.class, pregId));
    }

    void editPregnancy(int pregId) {
        startActivity(prepareIntent(ANewPregnancy.class, pregId));
    }

    Intent prepareIntent(Class<?> className, int pregId) {
        Intent i = new Intent(this, className);

        if (pregId > 0) {
            PregnanciesDbAdapter db = new PregnanciesDbAdapter(this);
            db.open();
            Pregnancy p = db.load(pregIds.get(pregId));
            db.close();
            i.putExtra(C.EXTRA_PREGNANCY, p);
        }

        return i;
    }
}

感谢阅读。希望你能帮忙。

1 个答案:

答案 0 :(得分:4)

哦,天哪,再一次。我发现自己该怎么做,这比简单容易。

AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
// info.position is the position in the ListView

我讨厌自己:)