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

时间:2018-07-04 05:21:38

标签: java android android-recyclerview

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

RequestQueue requestQueue = Volley.newRequestQueue(getContext());

    StringRequest stringRequest = new StringRequest(Request.Method.GET, projectDetailUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response)
        {


               progressBar.setVisibility(View.GONE);

               GsonBuilder builder = new GsonBuilder();
               Gson mGson = builder.create();
               posts =new ArrayList<>(Arrays.asList(mGson.fromJson(response, ProDetail[].class)));

               adapter = new OnSiteAdapter( posts);
               recyclerView.setAdapter(adapter);
               final List<ProDetail> finalPosts = posts;
               ItemClickSupport.addTo(recyclerView).setOnItemClickListener(
                       new ItemClickSupport.OnItemClickListener()
                       {
                           @Override
                           public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                               // do it
                               String deviceId = finalPosts.get(position).getDeviceId();
                               Intent proDetailIntent = new Intent(getContext(), SensorDetailAcitivity.class);
                               proDetailIntent.putExtra("deviceid", deviceId);
                               startActivity(proDetailIntent);


                           }
                       }
               );

           }



    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            progressBar.setVisibility(View.GONE);
            Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();

        }
    });
    requestQueue.add(stringRequest);

2 个答案:

答案 0 :(得分:1)

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

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));
    }
}

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

答案 1 :(得分:0)

您应该创建两个单独的列表,并用它们更新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);
     }
 }