首页 > 解决方案 > 从 mikepenz 材料抽屉中的数组中获取配置文件项目

问题描述

如您所见,有一些示例配置文件项目绑定到抽屉进行测试(它由 制成IProfile),但我想从中获取它们arraylist<user>

这是示例代码:

final IProfile profile = new ProfileDrawerItem().withName(LoginActivity.loggedInUser.get(0).getMobile()+"").withEmail(LoginActivity.loggedInUser.get(0).getResponse()).withIdentifier(100);
    final IProfile profile2 = new ProfileDrawerItem().withName("Bernat Borras").withEmail("alorma@github.com").withIdentifier(101);
    final IProfile profile3 = new ProfileDrawerItem().withName("Max Muster").withEmail("max.mustermann@gmail.com").withIdentifier(102);
    final IProfile profile4 = new ProfileDrawerItem().withName("Felix House").withEmail("felix.house@gmail.com").withIdentifier(103);
    final IProfile profile5 = new ProfileDrawerItem().withName("Mr. X").withEmail("mister.x.super@gmail.com").withIdentifier(104);
    final IProfile profile6 = new ProfileDrawerItem().withName("Batman").withEmail("batman@gmail.com").withIdentifier(105);

此配置文件将添加到accountHeaderaddProfiles(profile,profile1,...);方法。

但我想从数组中设置配置文件项...我正在尝试IProfile从我的用户中创建一个数组:

喜欢以这种方式获取个人资料:

 if(!Arrays.userCars.isEmpty()) {
      for (int i = 0; i <= Arrays.userCars.size(); i++) {
        Arrays.profiles.add(new ProfileDrawerItem()
          .withName(Arrays.userCars.get(i).getName())
          .withEmail(Arrays.userCars.get(i).getCarCompany().toString())
          .withIdentifier(103 + i));
    
      }
    }

profiles但是如何从Array中获取这些项目并在addProfiles()方法中使用?请记住,此方法来自库,我无法更改。

标签: androidarraysnavigation-drawerprofilematerialdrawer

解决方案


MaterialDrawer 库公开了一个函数来将配置文件配置为vararg参数,因此使用起来更方便。

例如,如果您想提供项目,array可以使用*Kotlin 中的 (spread operator)。

val myArray = arrayOf(profile, profile2, profile3)
addProfiles(*myArray)

推荐阅读