如何取决于哪一项长期$ P $在ListActivity pssed设置特定的上下文菜单? [英] How to set specific context menu depending on which item is long-pressed in a ListActivity?

查看:140
本文介绍了如何取决于哪一项长期$ P $在ListActivity pssed设置特定的上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表的活动,我选择手动添加这是第一项添加新项...。

I have a list activity, and I chose to manually add the first item which is "add new item...".

我已经注册的上下文菜单整个列表视图,使用 registerForContextMenu(getListView()); 直入的onCreate

I have registered the context menu for the whole listview, using registerForContextMenu(getListView()); straight into the onCreate.

在上下文菜单构建,系统调用 onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo)。在视图V 是ListView的,我不能找到一个办法知道哪些在ListView项目被长期pressed。

When the context menu is build, the system calls onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo). The View v is the listView, and I cannot find a way to know which item in the listview is being long-pressed.

我可以后创建一个XML布局,为布局添加新项...,然后添加一个ListView,这将在活动进行填充,这将上下文菜单反应,但我肯定有解决这个问题,没有任何XML布局的一种方式。

I could create a xml layout, with a layout for the "add new item..." and add a listview after, which would be populated by the activity, and that would react to the context menu, but I'm sure there is a way to solve this problem without any xml layout.

我曾尝试使用 registerForContextMenu ,其工作每个视图登记列表视图我的里面,但是列表视图不响应了触摸。

I have tried to register each view inside my listview using registerForContextMenu, which works, however the listview doesn't respond to touch anymore.

下面是我的活动code房源的:

Here is my activity code listing:

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;
    }
}

感谢您的阅读。希望能帮到你。

Thanks for reading. Hope you can help.

推荐答案

哦,上帝,一次。我发现自己如何做到这一点,这是比简单的更容易。

Oh, god, again. I found out myself how to do it, and it was easier than easy.

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

我恨我自己:)

这篇关于如何取决于哪一项长期$ P $在ListActivity pssed设置特定的上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆