2014-10-20 57 views
-3

我JSON對象是這樣的:如何根據另一個對象值過濾Android中的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

我真的不明白你真正的問題是什麼? – 2014-10-20 09:06:27

回答

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()    
     } 
+0

我可以記錄數據,但無法將其顯示在視圖中:1(news)[[email protected],[email protected],com .sri.vaave.data.FeedItem @ 527f96e0,[email protected],[email protected]] – Srikanth 2014-10-20 09:15:39

+0

我不明白你想說什麼,但如果它不起作用,你應該檢查你的適配器實現。 – furkan3ayraktar 2014-10-20 09:17:30

+0

適配器實現:http://pastie.org/9662542 – Srikanth 2014-10-20 09:21:01