【问题标题】:Android how to populate details in adapter by using arraylist of modal classAndroid如何使用模态类的arraylist填充适配器中的详细信息
【发布时间】:2019-05-07 12:55:19
【问题描述】:

我在我的项目中使用 recyclerview,我必须在 recycler 项目中填充不同的数据列表,因此我为每个列表创建了不同的模型类并集成为单个模型类现在我想填充 recyclerview 中的所有列表项

这是模型类列表的arraylist

 ArrayList profileArr =new ArrayList();
 profileArr.add(profileDataList);
 profileArr.add(userWorksClassList);
 profileArr.add(userEducationClassList);
 profileArr.add(userSportsClassList);
 profileArr.add(userDanceClassList);
 profileArr.add(userMusicClassList);
 profileArr.add(userMusicInstruClassList);
 profileArr.add(userHobbiesClassList);
 profileArr.add(userClubClassList);
 profileArr.add(userEmergencyContClassList);
 profileArr.add(userBloodGroupClassList);

这里我将值传递给适配器

 ProfileUiAdapter profileUiAdapter=new
 ProfileUiAdapter(profileArr,profileTitle);

这是我试图在我的适配器类中获取值的代码

 private ArrayList profileArr;
 private List<String> profileTitle;
 Object profile;

 if(position==0){
    profile = profileArr.get(position);
    ProfileMainmod profileMainmod= (ProfileMainmod) profile;
    String strEmail=profileMainmod.email;
    Log.e("profile", strEmail);
 }

我希望像 strEmail 这样的输出应该包含来自 arraylist 的值。

【问题讨论】:

  • 到底是什么问题?这看起来不错,那么它在哪里崩溃或无法按预期工作,以及发生了什么?
  • 它的说法数组不能转换为模型类
  • 字符串 strEmail=profileMainmod.email;此行越来越 arralist 无法转换为模型类

标签: java android


【解决方案1】:

对,所以看起来你实际上是在创建一个包含其他列表的数组列表

ArrayList profileArr =new ArrayList();
 profileArr.add(profileDataList);

profileDataList 是 ProfileMainmod 对象的数组吗?

所以这段代码应该可以工作:

ArrayList<ProfileMainmod> profileList = (ArrayList<ProfileMainmod>)profileArr.get(position);
ProfileMainmod profileMainmod= (ProfileMainmod) profileList.get(0);

但您需要决定和/或解释如何处理列表中的索引。

【讨论】:

  • 抱歉回复晚了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2014-04-26
  • 2022-09-23
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
相关资源
最近更新 更多