MBehtemam
MBehtemam

Reputation: 7919

Set drawer items from array list and clicks in MaterialDrawer

in my android application i'm using greeDAO ORM for sqlite. so i retrive my category list :

 List<ArticleCategory> categories = articleCategoryDao.queryBuilder().list(); 

each ArcticleCategory object has name and description property so i can use these for name and description of drawer items.my question is how to add this list to myDrawer items and how to manage their click's event. and this is my drawer codes :

    Drawer myDrawer = new DrawerBuilder().withActivity(this).withToolbar(toolbar)
            .addDrawerItems(
                    new PrimaryDrawerItem().withName("Home").withIcon(GoogleMaterial.Icon.gmd_import_contacts).withIconColor(Color.BLACK)
            )
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {

                    return false;
                }
            })
            .withAccountHeader(navigationHeader)
            .withDrawerGravity(GravityCompat.START)
            .build();

please keep in my mind i have some static items that i manually and items to drawer and some dynamic that come from database so click event is very important for me in this scenario.

Upvotes: 1

Views: 772

Answers (1)

mikepenz
mikepenz

Reputation: 12868

To add the Categories to the drawer you first have to create the DrawerItems you can do this by iterating over your items.

ArrayList<IDrawerItem> drawerItems = new ArrayList<>();
for(ArticleCategory category : categories) {
   drawerItems.add(new PrimaryDrawerItem().withName(category.getName()).withDescription(category.getDescription()));
   //if you have a id you can also do: .withIdentifier(category.getIdentifier());
   //depending on what you need to identify or to do the logic on click on one of those items you can also set a tag on the item: .withTag(category);
}

After you have created your items you add them to your DrawerBuilder

drawerBuilder.withDrawerItems(drawerItems);

Now as the drawer is created you have to write the logic for the Listener. Your "static" DrawerItems should define an identifier so you can directly react if one of those is clicked

.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
  @Override
  public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
      if (drawerItem != null) {
          Intent intent = null;
          if (drawerItem.getIdentifier() == 1) {
              //static item with ID 1
          } else if (drawerItem.getIdentifier() == 2) {
              //static item with ID 2
          } else {
              //if none of your static items were clicked handle the logic for the categories. 
              //now you have the drawerItem which were created from a category
              //you can identify them by identifier, their tag, or name. Depends on what you need to do your logic here

          }
      }

      return false;
  }
})

Upvotes: 5

Related Questions