如何使用过滤器删除json列表中的对象?

时间:2015-01-07 15:24:13

标签: java android arrays json

我想用过滤器删除数组中的某些对象。

这是生成数组的代码(JSON解析器)

protected Boolean doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();
            JSONArray json = jParser.getJSONFromUrl(url);

            for (int i = 0; i < json.length(); i++) {

                try {
                    JSONObject c = json.getJSONObject(i);

                    String naam = c.getString("actNaam");

                    String type = c.getString("actType");
                    String datum = c.getString("actDatum");

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put("actNaam", naam);
                    map.put("actType", type);
                    map.put("actDatum", datum);


                    jsonlist.add(map);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return null;

        }
    }

这是应该过滤数组的代码,用于显示。

@Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }

            for (int i = 0; i < jsonlist.size(); i++) {
                if (wandeling == false && type == "wandeling") {
                //Here we want to remove the types that have the string wandeling
                //The boolean wandeling has to be false befor the items should be removed
                }
                if (fiets == false && type == "fiets") {
                //Here we want to remove the types that have the string wandeling
                }
                if (excursie == false && type == "excursie") {
                //Here we want to remove the types that have the string wandeling
                }
            }

            ListAdapter adapter = new SimpleAdapter
                    (context, jsonlist, R.layout.list_activity,

                            new String[]{naam, type, datum},
                            new int[]{R.id.ActNaam, R.id.ActType, R.id.Actdatum});

            setListAdapter(adapter);

            lv = getListView();

        }

我们正在从json文件中创建一个列表。该列表有3个项目:naam,type和datum。 我们希望用户能够通过按钮“过滤”列表。该按钮是回馈wandeling,fiets和excursie的布尔值的按钮。

因此,例如当没有点击“wandeling”按钮(false)时,我们想要删除阵列中具有“wandeling”类型的每个项目; 这些类型在json文件中使用如下字符串进行安全保护:

"actType":"wandeling"
"actType":"fiets"
"actType":"excursie"

抱歉我的英语不好,谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

Peraps你应该使用一个类而不是一个map并使用java8的流api

result = jsonlist.stream()
  .filter( x -> wandling || (!x.actType.equals("wandling") )
  .filter( x -> fiets || (!x.actType.equals("fiets") )
  .filter( x -> excursie|| (!x.actType.equals("excursie") )
  .collect(Collectors.toList())

看起来应该是

答案 1 :(得分:0)

这就是我做了它的作品!

 for (int i = jsonlist.size()-1 ; i >= 0; i--) {
                    if (jsonlist.get(i).get(type).equals("wandeling") && wandeling == false) {
                        jsonlist.remove(i);
                    }
                }