如何根据对象的另一个值过滤Android中的JSON对象?

时间:2014-10-20 09:03:02

标签: android json

我这样的json对象:

JSon对象

{
      "31": {
        "basic": {
          "node_id": "31",
          "title": "test",
          "alias": "test",
          "description": "test",
          "site_id": "151336557",
          "node_type": "7",
          "privacy": "7",
          "deleted": "0",
          "status": "1",
          "created_date": "1379169518",
          "updated_date": "1379169518",
          "created_by": "140513626519686828",
          "updated_by": null,
          "readable_date": "14th Sep, 2013"
        },
        "meta": {
          "forum_id": "61"
        },
        "comments": {
          "count": 1
        },
        "likes": {
          "count": 0
        },
        "tags": [],
        "node_id": "31"
      },
      "32": {
        "basic": {
          "node_id": "32",
          "title": "testing discussion",
          "alias": "testing-discussion",
          "description": "testing",
          "site_id": "151336557",
          "node_type": "7",
          "privacy": "7",
          "deleted": "0",
          "status": "1",
          "created_date": "1379493816",
          "updated_date": "1379493816",
          "created_by": "140513795022034166",
          "updated_by": null,
          "readable_date": "18th Sep, 2013"
        },
        "meta": {
          "forum_id": "65"
        },
        "comments": {
          "count": 1
        },
        "likes": {
          "count": 0
        },
        "tags": [],
        "node_id": "32"
      }     
}

这是我想要得到某个对象的代码:

           arrayTemplist = new ArrayList<FeedItem>();

            for(int x=0;x<feedItems.size();x++)
            {

                int currentNodeType =feedItems.get(x).getNode_type();

                if (currentNodeType!=0 && currentNodeType==nodeType)
                {
                    arrayTemplist.add(feedItems.get(x));
                    Log.d("Cem", arrayTemplist.toString());
                }
                else{


                    listAdapter = new FeedListAdapter(this, feedItems);
                    listView.setAdapter(listAdapter);
                    listView.setEmptyView(findViewById(R.id.empty));
                }


            }

            listAdapter = new FeedListAdapter(this, arrayTemplist);
            listView.setAdapter(listAdapter);
            listView.setEmptyView(findViewById(R.id.empty));

            }

我无法显示已过滤数据的视图。我可以记录数据,但我无法显示它。

整个代码在http://pastie.org/9662511

1 个答案:

答案 0 :(得分:0)

首先,您不应该每次迭代都设置适配器。其次,您应该调用BaseAdapter的notifyDataSetChanged方法来刷新列表视图中显示的数据。

       arrayTemplist = new ArrayList<FeedItem>();

        for(int x=0;x<feedItems.size();x++)
        {

            int currentNodeType =feedItems.get(x).getNode_type();

            if (currentNodeType!=0 && currentNodeType==nodeType)
            {
                arrayTemplist.add(feedItems.get(x));
                Log.d("Cem", arrayTemplist.toString());
            }
        }

        listAdapter = new FeedListAdapter(this, arrayTemplist);
        listView.setAdapter(listAdapter);
        listView.setEmptyView(findViewById(R.id.empty));

        listAdapter.notifyDataSetChanged()            
        }