Search code examples
javaandroidobjectdrawernavigationview

Retrieve OBJECT from a navigation View (drawer) dynamically android/java


Once again stuck dead in my coding tracks, I come seeking knowledge from the omniscient StackOverflow Community... My problem is fairly simple. I have populated a Navigation View drawer with objects. These objects toString() methods have been overwritten to display each objects own name in the list. Clicking each object in the drawer list brings up an alert message that should say ("you clicked "+ myDrawerObject.getName()) The problem is in the onNavigationItemSelected listener. This method returns a MenuItem item which is a view. I am having trouble understanding why i can not treat this view like the object it is supposed to contain. Help is greatly appreciated and thanks in advance!

I understand that the item parameter is returning which menu item has been clicked but if the menu item is an object why cant I call those objects methods? I have tried grabbing the item return value and calling its getName() method, to no avail. item.getName(); (I understand that item is a view so it doesnt have a "getName()" method

I have tried casting the item value back into an object to no avail MyObject myObject = (MyObject)item; //Item is still a view and a view cant be cast into an object it seems

I have even tried putting all created objects of the MyObject type into a static arrayList then trying to match up id's also to no avail. //in the onNavigationItemSelected listener int id = item.getItemId()

//somewhere else in the code
 for (anotherMyObject : listOfEveryMyObjectEverMade){
 anotherMyObject.get(id)
 }
/*returns the id of the item in the listview. as i understand a specific id 
is assigned to every item in the list. so if an object has never been in that 
particular list, it wont have that same id
*/

I must add the the contents of the drawer (the string list) was created dynamically so there is no xml file for the menu or menItems!


Solution

  • I ran into a very similar situation and literally blockaded me for days!

    I'm almost 90% positive that there has to be a way out there...

    but I personally didn't find it, so instead I went with a work around:

    private void setMenus() {
        int temp = 0;
      //Mine was inside of a fragment, hints the getActivity() call
        NavigationView navView22 = getActivity().findViewById(R.id.navigation_view);
        MenuItem oneItem = navView22.getMenu().findItem(0);
            if (oneItem == null){
            Menu m = navView22.getMenu();
            SubMenu sm0 = m.addSubMenu(
                    getResources().getString(R.string.clickToSelectRookies));
            sm0.add(0,
                    temp, temp, getResources().getString(R.string.signAllString));
                tempRooks.add(round1.get(0));
      /*I had to create the entire menu dynamically, so I did so by creating 
      subMenus in my menus in numerical order
      I then created an arraylist to hold all my objects in that same order
      though in retrospect I should have just used a HashMap!!
      finally in the onClick I was able to listen for which subMenu item was clicked
       and since the menu's and my arraylist of objects corresponded I then pulled 
      the correct object from my arraylist. I realize this is a little hacky, but 
      hopefully if you do not find the answer you can do this temporarily.
      If you do find a better solution please post so we all can learn. Thank YOU!!
    

    */ temp++;

            SubMenu sm1 = m.addSubMenu(
                    getResources().getString(R.string.firstRoundPicks));
            for (int x = 0; x < round1.size(); x++) {
                sm1.add(0, temp, temp, round1.get(x).toString());
                    tempRooks.add(round1.get(x));
                temp++;
            }