如何在单击的上下文菜单中获取单击列表视图的位置

时间:2013-05-15 02:09:05

标签: android listview contextmenu

我在xml中有一个已定义的列表视图,并通过适配器修改它来添加数据。 我想通过单击列表显示上下文菜单,我已经完成了... 但是如何获得点击列表的位置?

这是代码,这就是我调用上下文菜单的方式:

list = (ListView)findViewById(R.id.grant_list);

    // get all data in database
    List<GrantListData> dataList = con.getAllList();  
    try {
        list.removeAllViews();
    } catch (Exception e) {}

    listAll.clear();
    arrName.clear();
    arrPhone.clear();
    for (int i = 0; i < dataList.size(); ++i) {
          listAll.add(dataList.get(i));
          arrName.add(dataList.get(i).getName().toString());
          arrPhone.add(dataList.get(i).getPhone().toString());
    }

    adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, arrName);
    list.setAdapter(adapter);

    this.registerForContextMenu(list);
    list.setOnLongClickListener(this);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            String item = (String)parent.getItemAtPosition(pos);
            activity.registerForContextMenu(view);
            activity.openContextMenu(view);
        }
    });

这是onCreateContextMenu方法:

int num=-1;
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
      //String clicked = adapter.getItem(Info.position); --> this is when I get confused
      //num = Info.position;
      //menu.setHeaderTitle("Options for " + clicked);
      menu.add(1, 1, 1, "Details");
      //menu.addSubMenu(1, 1, 1, arrName.get(Info.position));
      //menu.addSubMenu(1, 1, 2, arrPhone.get(Info.position));
      menu.add(1, 2, 2, "Delete");
}

[ADDITION]

private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

提前致谢:)

[解决] 嗯,我花了一段时间才意识到简单的事情。我的意思是这个

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            currList = (String)parent.getItemAtPosition(pos);
            currIndex = pos;

            registerForContextMenu(view);
            openContextMenu(view);
        }
    });

和这个

int num=-1;
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
      menu.setHeaderTitle("Options for "+ currList);
      menu.add(1, 1, 1, "Details");
      menu.addSubMenu(1, 1, 1, arrName.get(currIndex));
      menu.addSubMenu(1, 1, 2, arrPhone.get(currIndex));
      menu.add(1, 2, 2, "Delete");
}

感谢你的回答:)

2 个答案:

答案 0 :(得分:0)

试试这个:

 ((ListView) findViewById(R.id.lisview)).setOnItemClickListener(new OnItemClickListener(){

                    public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
                            long arg3) {
                        // TODO Auto-generated method stub
                        /*"Toast the int which is arg2 to show the position of listview that has been clicked"*/


                    }

                });


Using Context menu:

    public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                super.onCreateContextMenu(menu, v, menuInfo);
                menu.setHeaderTitle("Do you want to add?");

                MenuItem chix = menu.add("Add Chicken");
                MenuItem pasta = menu.add("Add Pasta");
                MenuItem drinks = menu.add("Add Drinks");
                MenuItem con = menu.add("Continue");
                chix.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub
                        //your codes here//

                        return true;
                    }
                });

                pasta.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub
                        //your codes here//
                        return true;
                    }
                });

                drinks.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub
                        //your codes here//
                        return true;
                    }
                });

                con.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub
                        //your codes here//
                        return true;
                    }
                });
                }

答案 1 :(得分:0)

您为视图设置了一个带位置的标记,然后按此位置设置菜单项的groupid。

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        String item = (String)parent.getItemAtPosition(pos);
        view.setTag(position);
        activity.registerForContextMenu(view);
        activity.openContextMenu(view);
    }
});

将位置放在菜单项的groupid中,您可以通过getGroupId

检索该位置
@Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
  //String clicked = adapter.getItem(Info.position); --> this is when I get confused
  int position = Integer.parseInt(v.getTag());
  //num = Info.position;
  //menu.setHeaderTitle("Options for " + clicked);
  menu.add(position, 1, 1, "Details");
  //menu.addSubMenu(1, 1, 1, arrName.get(Info.position));
  //menu.addSubMenu(1, 1, 2, arrPhone.get(Info.position));
  menu.add(position, 2, 2, "Delete");
}