简体   繁体   English

如何使用2个数组创建导航抽屉

[英]How to create navigation drawer with 2 array

I have two array like below 我有如下两个数组

DB_id = [1,3,5,6,9]

DB_name = [a,b,c,d,e]

I set DB_name array for my navigation drawer list like this : 我为导航抽屉列表设置DB_name数组,如下所示:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1,name);

but I don't know how can I set DB_id as pos or id 但我不知道如何将DB_id设置为pos或id

public void onItemClick(AdapterView<?> parent, View view, final int pos, final long id){
            drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                @Override
                public void onDrawerClosed(View drawerView){
                    super.onDrawerClosed(drawerView);
                    String PosTxt = Integer.toString((pos+1));
                    aString = "SELECT * FROM MainContent WHERE GroupID LIKE '"+PosTxt+"'";
                    myBrowser.loadUrl("javascript:fill_comment()");
                }
            });
            drawer.closeDrawer(navList);
        }

you can achieve it with a subclass of ArrayAdapter 您可以使用ArrayAdapter的子类来实现

define a container class for your information, and override toString() to return name 为您的信息定义一个容器类,并覆盖toString()以返回名称

public static class Container {
   public int id;
   public String name;

   public String toString() {
      return name;
   }    
}

then define a subclass of ArrayAdater , and override getItemId to make it return the id 然后定义ArrayAdater的子类,并重写getItemId以使其返回id

public class MyAdapter extends ArrayAdapter<Container> {
        public MyAdapter(Context context, int resource, Container[] objects) {
            super(context, resource, objects);
        }

       @Override
        public long getItemId(int position) {
             return getItem(position).id;
        }
}

then create a Container[] objs , and initialize its content. 然后创建一个Container[] objs ,并初始化其内容。 Last step is to use the custom adapter 最后一步是使用自定义适配器

MyAdapter<Container> adapter new MyAdapter<Container>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, objs);

in onItemClick , use id instead of position onItemClick ,使用id代替position

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM