2016-12-11 52 views
1

所以我使用Json檢索一些數據,我得到的數據是正確的(根據日誌),但當我嘗試將數據推入列表視圖它似乎不工作。下面是代碼:無法傳遞數據到我的ListView後Json請求

public class MainActivity extends AppCompatActivity { 

    private List<newsItem> newsFeed = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     RequestQueue queue = Volley.newRequestQueue(this); 

     JsonObjectRequest myRequest = new JsonObjectRequest(Request.Method.GET, 
       "https://content.guardianapis.com/search?q=cancer%20new%20research&order-by=relevance&api-key=test", 
       null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         JSONObject responseNode = null; 
         try { 
          responseNode = response.getJSONObject("response"); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
         JSONArray newsItems = null; 
         try { 
          newsItems = responseNode.getJSONArray("results"); 
         } catch (JSONException e) { 
          Log.i("myTag4", e.toString()); 
         } 
         for (int i = 0; i < newsItems.length(); i++) { 
          try { 
           JSONObject temp = newsItems.getJSONObject(i); 
           String title = temp.getString("webTitle"); 
           Log.i("Titles", title); 
           newsFeed.add(new newsItem(title, "This is a small desc", "Jan 1 1999", "12:00", "http://google.gr", R.mipmap.ic_launcher)); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.i("Error", error.toString()); 
      } 
     }); 

     queue.add(myRequest); 
     ArrayAdapter<newsItem> adapter = new customAdapter(); 
     ListView newsItemsListView = (ListView) findViewById(R.id.newsListView); 
     newsItemsListView.setAdapter(adapter); 


    } 

    private class customAdapter extends ArrayAdapter<newsItem> { 
     public customAdapter() { 
      super(MainActivity.this, R.layout.item, newsFeed); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      if (convertView == null) { 
       convertView = getLayoutInflater().inflate(R.layout.item, parent, false); 
      } 

      newsItem currentItem = newsFeed.get(position); 

      ImageView newsImage = (ImageView) convertView.findViewById(R.id.leftIco); 
      TextView desc = (TextView) convertView.findViewById(R.id.desc); 
      TextView heading = (TextView) convertView.findViewById(R.id.heading); 

      desc.setText(currentItem.getNewsDesc()); 
      heading.setText(currentItem.getNewsHeading()); 
      newsImage.setImageResource(currentItem.getImageID()); 

      return convertView; 
     } 
    } 
} 

,並將日誌:

12-11 18:33:18.134 20517-20517/com.example.nikom.medicalnews I/Titles: Pollution may shorten lung cancer patients' lives, research shows 
12-11 18:33:18.134 20517-20517/com.example.nikom.medicalnews I/Titles: Why Brexit is bad news for cancer research 
12-11 18:33:18.134 20517-20517/com.example.nikom.medicalnews I/Titles: How nanotechnology research could cure cancer and other diseases 
12-11 18:33:18.134 20517-20517/com.example.nikom.medicalnews I/Titles: US Zika funding shortfall will impede cancer research, health officials say 
12-11 18:33:18.134 20517-20517/com.example.nikom.medicalnews I/Titles: 'Moonshot' cancer panel calls for US to create national research database 
12-11 18:33:18.134 20517-20517/com.example.nikom.medicalnews I/Titles: Stress and trauma are not causes of breast cancer, research rules 
12-11 18:33:18.135 20517-20517/com.example.nikom.medicalnews I/Titles: Academics feel blight of Brexit – from cancer research to peat projects 
12-11 18:33:18.135 20517-20517/com.example.nikom.medicalnews I/Titles: Cancer, sea life, mental health: the UK research that will be hit by Brexit 
12-11 18:33:18.135 20517-20517/com.example.nikom.medicalnews I/Titles: Cancer rates up 12% in 20 years, say Cancer Research UK 
12-11 18:33:18.135 20517-20517/com.example.nikom.medicalnews I/Titles: Samuel Johnson quits acting to focus on campaigning for cancer research 

正如你可以看到我得到正確的標題,但他們只是出現在列表視圖!

回答

2
for (int i = 0; i < newsItems.length(); i++) { 
      try { 
        JSONObject temp = newsItems.getJSONObject(i); 
        String title = temp.getString("webTitle"); 
        Log.i("Titles", title); 
        newsFeed.add(new newsItem(title, "This is a small desc", "Jan 1 1999", "12:00", "http://google.gr", R.mipmap.ic_launcher)); 
        adapter.notifyDataSetChanged(); 
        } catch (JSONException e) { 
          e.printStackTrace(); 
        } 
} 

每次你得到你應該告訴它的數據「嘿,我有一個新的數據,讓我們通知,然後顯示它」

+0

但沒有「轉接器」裏面,我該如何使用它,然後? – Nick

+1

使'ArrayAdapter adapter'類字段或在JsonObjectRequest'聲明之前移動它。 – jereksel

1

將數據添加到列表後,必須在適配器上調用notifyDataSetChanged()

相關問題