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

我在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代码。


慕村9548890
浏览 149回答 2
2回答

江户川乱折腾

您应该创建两个单独的列表并RecyclerView使用它们进行更新Java 8:List<PostDetail> onlinePostDetail = posts.stream()&nbsp; &nbsp; .filter(p -> p.getDeviceStatus().equals("Online"))&nbsp; &nbsp; .collect(Collectors.toList());List<PostDetail> offlinePostDetail = posts.stream()&nbsp; &nbsp; .filter(p -> p.getDeviceStatus().equals("Offline"))&nbsp; &nbsp; .collect(Collectors.toList());在Java 8以下:&nbsp;List<PostDetail> onlinePostDetail = new ArrayList();&nbsp;List<PostDetail> offlinePostDetail = new ArrayList();&nbsp;for(PostDetail p: posts) {&nbsp; &nbsp; &nbsp;if(p.getDeviceStatus().equals("Online")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;onlinePostDetail.add(p);&nbsp; &nbsp; &nbsp;} else if(p.getDeviceStatus().equals("Offline")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;offlinePostDetail.add(p);&nbsp; &nbsp; &nbsp;}&nbsp;}

翻阅古今

您可以基于帖子列表创建两个列表List<PostDetail> onlineList = new ArrayList<>();List<PostDetail> offlineList = new ArrayList<>();for(int i = 0; i < posts.size(); i++) {&nbsp; &nbsp; if(posts.get(i).getDeviceStatus.equals("Online")){&nbsp; &nbsp; &nbsp; &nbsp; onlineList.add(posts.get(i));&nbsp; &nbsp; } else if(posts.get(i).getDeviceStatus.equals("Offline")) {&nbsp; &nbsp; &nbsp; &nbsp; offlineList.add(posts.get(i));&nbsp; &nbsp; }}然后,您可以在不同的屏幕或任何需要的地方显示这两个列表
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java