我正在尝试通过从数据库获取记录并在左侧抽屉中显示来创建导航抽屉。
通过执行以下操作我能够成功完成,但问题是,当在导航抽屉中单击菜单项时,我想获得它的标题。但我只是得到它的立场。
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
List<DataItems> myTest = myDbAdapter.getValuesFromDb(); // method in DbAdapter class
// to get records from db.
// Pass the data to ListView adapter
MyNavigationDrawerAdapter adapter = new MyNavigationDrawerAdapter(
this,
R.layout.drawer_listview_item,
myTest);
// Set the adapter for the list view
mDrawerList.setAdapter(adapter);
// set the item click listener
mDrawerList.setOnItemClickListener(new NavigationDrawerItemClickListener());
// navigation drawer click listener
private class NavigationDrawerItemClickListener implements ListView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Log.i("Position", "" + position); // prints the position successfully
Log.i("Value", "" + myTest.get(position).getTitle()); // prints null
}
}
你能告诉我,为什么这个 myTest.get(position).getTitle()这是null。我在这里缺少什么?
答案 0 :(得分:0)
使用
List<DataItems> myTest = new List<DataItems>(myDbAdapter.getValuesFromDb());
而不是使用
List<DataItems> myTest = myDbAdapter.getValuesFromDb();
答案 1 :(得分:0)
在onClickListener中尝试:
public void onItemClick(AdapterView <?> parent, View view, int position, long id){
DataItems dataItem = adapter.getItem(position);
String mString = dataItem.getTitle();
}