为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Android中通过字符串值过滤json对象?

如何在Android中通过字符串值过滤json对象?

慕村9548890 2021-05-05 10:07:15
我在recyclerview中具有带片段的查看寻呼机,我想基于第一个片段中的值对解析json数据,如果设备在线,而第二个数据基于脱机值,则我想对所有数据进行任何建议,我们将不胜感激。我的Json价值。[{    "deviceId": "IVIS1475",    "siteName": "IVIS1475    SBI-CHE-Coimbatore-Kangeyampalayam",    "deviceStatus": "Offline",    "hddStatus": "Ok",    "powerSource": "Battery"},{    "deviceId": "IVIS1354",    "siteName": "IVIS1354    SBI-Trichy S.P Office Pudukottai",    "deviceStatus": "Offline",    "hddStatus": "Ok",    "powerSource": "Battery"},{    "deviceId": "IVIS1476",    "siteName": "IVIS1476    SBI-Coimbatore Redfields Coimbatore",    "deviceStatus": "Offline",    "hddStatus": "Ok",    "powerSource": ""},{    "deviceId": "IVIS1355",    "siteName": "IVIS1355    SBI-CHE-Pondicherry-Thiruvandarkoil Busstand",    "deviceStatus": "Online",    "hddStatus": "Not Ok",    "powerSource": ""},{    "deviceId": "IVIS1597",    "siteName": "IVIS1597    SBI-Chennai 1 Melvisharam Vellore",    "deviceStatus": "Online",    "hddStatus": "Ok",    "powerSource": "Mains"},{    "deviceId": "IVIS2203",    "siteName": "IVIS2203    SBI-Coimbatore Kutchery Strt Udamalpet",    "deviceStatus": "Online",    "hddStatus": "Ok",    "powerSource": "Battery"},{    "deviceId": "IVIS1478",    "siteName": "IVIS1478    SBI-Coimbatore Officers Club ",    "deviceStatus": "Offline",    "hddStatus": "Ok",    "powerSource": "Battery"},{    "deviceId": "IVIS2205",    "siteName": "IVIS2205    SBI-Coimbatore Municipal Cmplx Udamalpet",    "deviceStatus": "Offline",    "hddStatus": "Ok",    "powerSource": "Battery"},{    "deviceId": "IVIS2206",    "siteName": "IVIS2206    SBI-Coimbatore Railway Station Tirupur",    "deviceStatus": "Offline",    "hddStatus": "Not Ok",    "powerSource": "Battery"},{    "deviceId": "IVIS2207",    "siteName": "IVIS2207    SBI-Coimbatore Dr Nanjappa Rd Coimbatore",    "deviceStatus": "Online",    "hddStatus": "Not Ok",    "powerSource": "Battery"}]基于设备状态,我想过滤json数据并将其添加到recyclerivew中,这是我的recyclerveiw代码。
查看完整描述

2 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

您应该创建两个单独的列表并RecyclerView使用它们进行更新


Java 8:


List<PostDetail> onlinePostDetail = posts.stream()

    .filter(p -> p.getDeviceStatus().equals("Online"))

    .collect(Collectors.toList());

List<PostDetail> offlinePostDetail = posts.stream()

    .filter(p -> p.getDeviceStatus().equals("Offline"))

    .collect(Collectors.toList());

在Java 8以下:


 List<PostDetail> onlinePostDetail = new ArrayList();

 List<PostDetail> offlinePostDetail = new ArrayList();

 for(PostDetail p: posts) {

     if(p.getDeviceStatus().equals("Online")) {

         onlinePostDetail.add(p);

     } else if(p.getDeviceStatus().equals("Offline")) {

         offlinePostDetail.add(p);

     }

 }


查看完整回答
反对 回复 2021-05-12
?
翻阅古今

TA贡献1780条经验 获得超5个赞

您可以基于帖子列表创建两个列表


List<PostDetail> onlineList = new ArrayList<>();

List<PostDetail> offlineList = new ArrayList<>();

for(int i = 0; i < posts.size(); i++) {

    if(posts.get(i).getDeviceStatus.equals("Online")){

        onlineList.add(posts.get(i));

    } else if(posts.get(i).getDeviceStatus.equals("Offline")) {

        offlineList.add(posts.get(i));

    }

}

然后,您可以在不同的屏幕或任何需要的地方显示这两个列表


查看完整回答
反对 回复 2021-05-12
  • 2 回答
  • 0 关注
  • 148 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信