简体   繁体   English

如何从导航抽屉菜单项获取标题

[英]How to get the title from Navigation drawer menu item

I am trying to create a Navigation drawer by getting records from database and showing them in left drawer. 我正在尝试通过从数据库获取记录并将其显示在左侧抽屉中来创建导航抽屉。

By doing the following I am able to do it successfully, but the problem is, when a menu item is clicked in Navigation drawer I want to get the title of it. 通过执行以下操作,我能够成功完成此操作,但是问题是,当在导航抽屉中单击菜单项时,我想获取其标题。 But I am only getting the position of it. 但是我只是得到它的位置。

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
   }
}

Can you please tell me, why this myTest.get(position).getTitle() this is null. 您能否告诉我,为什么此myTest.get(position).getTitle()为null。 What am I missing here? 我在这里想念什么?

use 采用

List<DataItems> myTest = new List<DataItems>(myDbAdapter.getValuesFromDb());

instead of using 而不是使用

List<DataItems> myTest = myDbAdapter.getValuesFromDb();

In your onClickListener try: 在您的onClickListener中,尝试:

 public void onItemClick(AdapterView <?> parent, View view, int position, long id){

  DataItems dataItem = adapter.getItem(position);
  String mString = dataItem.getTitle();

 }

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

相关问题 Android中的导航抽屉菜单项标题颜色 - Navigation Drawer Menu Item Title Color in Android 如何更改包含导航抽屉中菜单的项目的项目标题字体颜色 - How do I change Item title font color for an Item that contains a menu in Navigation Drawer 更改导航抽屉中菜单标题标题项的文本颜色 - Changing text color of menu heading title item in navigation drawer 如何在Android中以编程方式设置图标和导航抽屉菜单项标题之间的边距? - How to programmatically set the margin between icon and title of menu item of Navigation Drawer in Android? 固定顶部标题徽标并且滚动菜单项并显示在类别标题中时,如何创建左侧导航抽屉? - How to create left navigation drawer when top header logo is fixed and menu item is scrolling and show in category title? 如何以编程方式隐藏导航抽屉菜单项? - How to hide a navigation drawer menu item programmatically? 如何显示带有导航抽屉菜单项的指示器 - How to show an indicator with a navigation drawer menu item Android-如何刷新导航抽屉的菜单项 - Android - How to refresh the menu item of Navigation Drawer 导航抽屉项目标题颜色 - Navigation Drawer Item Title Color 根据抽屉项目的导航抽屉标题 - Navigation drawer title according to the Drawer item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM