Android ListView单击上下文菜单

时间:2012-01-18 05:04:29

标签: android listview

我有一个项目列表,每次短按我想要显示自定义上下文菜单。我已经查看了一些示例并尝试了很多东西,但我最接近的是长按一下菜单showup(并且每个项目的菜单并不明显)。这是代码,任何帮助都会很棒。请注意,在阅读了一系列教程之后,我将其从Activity中继承。

public class EventListActivity extends Activity {

      @Override
        public void onCreate(Bundle savedInstanceState) {
              Log.i("ME", "Event List Activity Into On Create ");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.event_list);
            Log.i("ME", "Event List Activity Into On Create 1 ");
            String[] values = new String[] {"Emerald Greens","Dahlgtreen","Edinborough","Pebble Creek","Gross"};
            Log.i("ME", "Event List Activity Into On Create 2");

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

            Log.i("ME", "Event List Activity Into On Create 2a");


            //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.event_list,R.id.label,values);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.event_list_item,values);

            Log.i("ME", "Event List Activity Into On Create 3");
            //setListAdapter(adapter);
            listView.setAdapter(adapter);
            Log.i("ME", "Event List Activity Into On Create 4");

            registerForContextMenu(listView);
            Log.i("ME", "Event List Activity Into On Create 6");

      }




    protected void onListItemClick(ListView listView, View v, int pos, long id) {
        String item = (String)listView.getAdapter().getItem(pos);
        Toast.makeText(this, item+" Selected pos:"+pos+":   id:"+id+":", Toast.LENGTH_LONG).show();


    }   


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        Log.i("ME", "OnCreateContextMenu ");
      if (v.getId()==R.id.list) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        //menu.setHeaderTitle(Countries[info.position]);
        menu.setHeaderTitle("Blippo");
        //String[] menuItems = getResources().getStringArray(R.array.menu);
        String[] menuItems = new String[] {"Menu a","Menub"};
        for (int i = 0; i<menuItems.length; i++) {
          menu.add(Menu.NONE, i, i, menuItems[i]);
        }
      }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
      int menuItemIndex = item.getItemId();
      //String[] menuItems = getResources().getStringArray(R.array.menu);
      String[] menuItems = new String[] {"Menu a","Menub"};
      String menuItemName = menuItems[menuItemIndex];

      //String listItemName = Countries[info.position];
      String listItemName = "hardcoded";

      Toast.makeText(this, item+" Selected Submenu", Toast.LENGTH_LONG).show();

      return true;
    }

    }

4 个答案:

答案 0 :(得分:2)

您可以使用滑动抽屉来点击每个项目,您可以打开滑动抽屉并显示您的项目

答案 1 :(得分:0)

它的android规则,只有当你对与之关联的contextMenu的视图进行长时间点击时才能弹出contextMenu。所以我想明确表示在任何其他事件上都不可能。

Second Problem--> menu was not distinct per item

您正在向ListView注册菜单,以便它如何检测该项目。当您长时间单击相同的contextMenu将会打开。当您单击任何行时,listView将打开相同的菜单。

答案 2 :(得分:0)

如果您想在单击时执行此操作,为什么不尝试打开自定义对话框,使用操作列表(选项),在列表项单击时打开此对话框,并在选择一个操作时将其关闭。 / p>

答案 3 :(得分:0)

上下文菜单仅适用于长按。如果您希望我为您提供代码以对列表项onclick()执行某些操作,并在列表项长按上打开上下文菜单,请告诉我。