0

I created a navigation drawer project in android studio and created the separate fragments for my each item in the list view in drawer. However, now the items in the list are not static ones. I have a map in which i have some values based on which i have to choose whether to show a particular item or not.

I tried to change the call to setAdapter method based on values in map. However, it breaks the code when clicked on a particular item because in my fragments i do setOnAttached with a certain position which is hard coded such as 0,1,2 .. My question is how can I change all of this to show the corresponding fragment when any item in drawer is clicked.

Is it possible to know that this fragment should be attached to the dynamically calculated position based on the list view that I get at run time ?

public void onAttach(Activity activity) {


    ((MyActivity)activity).onSectionAttached(4);
}

Thanks

kuldeep
  • 817
  • 10
  • 27
  • To show different item in the list you need to change the values in your dataset and call adapter.notifyDataSetChanged() method. – Radoslav Apr 15 '15 at 08:22
  • Thanks. I have one question though that if my items indexes are not constant in my navigation drawer, I mean some times the item1 may be at index 0 or some times since i dont have to show it in the drawer, the item2 index becomes 0 from index 1. This change in index result in updating the list on my drawer, but the fragments that gets loaded after clicking on these items are still same. For ex: in case i dont show item1 in my drawer then at 0 index still my fragment for item1 gets called, instead fragment for item2 should be called because item1 is not in the list anymore. – kuldeep Apr 15 '15 at 08:38
  • 1
    From what I understand you have hard time mapping the item clicked with the fragment you want to open. If that is your case you need to create custom adapter class use setTag() and getTag() methods. Here is explained that is their purpose. http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view – Radoslav Apr 15 '15 at 08:55
  • Thanks I checked it out, but i am still not able to understand how settag or gettag would solve my problem. :( , can you please give some rough idea in terms of code snippet ? – kuldeep Apr 15 '15 at 10:55
  • Thanks I could fix this , and it started working fine. I called setTag method in my custom adapter and accessing it onItemClickListener in createView for the listView. I check then the item name that i get from getTag and hence call relevant fragment for my listView that was clicked ! – kuldeep Apr 15 '15 at 16:18
  • 1
    I'm glad you are figure it out ;) – Radoslav Apr 16 '15 at 05:48

0 Answers0