Eugene Shmorgun
Eugene Shmorgun

Reputation: 2075

Android: how to assign the custom identifiers to dynamic menu items?

There is problem. In my app I have dynamicly built menu items in drawer menu.

    // data comes from database
    ArrayList<String> groupsName = mDbHelper.getAllGroupsName();

    Menu m = navigationView.getMenu();
    SubMenu userGroupsLessonsMenu = m.addSubMenu(R.string.your_groups);

    for (String groupName : groupsName) {
      userGroupsLessonsMenu.add(groupName);
    };

OK, it works good. Now I want to react on click on these items. I would like to assign my custom identifier to menu items in order in

    public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

just to set this id to intent and start new activity.

I don't know if it's important, but hardcoded menu items have specific identifiers, and my menu items always have 0:

06-25 17:50:20.857 23358-23358/my.package.name W/id: 2131493021
06-25 17:50:35.210 23358-23358/my.package.name W/id: 2131493022
06-25 17:50:38.372 23358-23358/my.package.name W/id: 2131493023
06-25 17:50:41.410 23358-23358/my.package.name W/id: 2131493024
06-25 17:50:47.576 23358-23358/my.package.name W/id: 0
06-25 17:50:51.903 23358-23358/my.package.name W/id: 0
06-25 17:50:57.241 23358-23358/my.package.name W/id: 0
06-25 17:51:00.788 23358-23358/my.package.name W/id: 0

Upvotes: 0

Views: 37

Answers (1)

R. Zag&#243;rski
R. Zag&#243;rski

Reputation: 20268

See this answer to create custom IDs. Then, assign them with the function setId.

If the list is very dynamic, then You can just assign numbers. ID is just an int.

Upvotes: 1

Related Questions