我有两个数组如下
DB_id = [1,3,5,6,9]
DB_name = [a,b,c,d,e]
我为我的导航抽屉列表设置DB_name
数组,如下所示:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1,name);
但我不知道如何将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);
}
答案 0 :(得分:1)
您可以使用ArrayAdapter的子类
来实现它为您的信息定义容器类,并覆盖toString()以返回名称
public static class Container {
public int id;
public String name;
public String toString() {
return name;
}
}
然后定义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;
}
}
然后创建一个Container[] objs
,并初始化其内容。最后一步是使用自定义适配器
MyAdapter<Container> adapter new MyAdapter<Container>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, objs);
<{1>}中的,使用onItemClick
代替id