在Json请求之后无法将数据传递给我的ListView

时间:2016-12-11 18:41:28

标签: java android json listview

所以我正在使用Json检索一些数据,我得到的数据是正确的(根据日志)但是当我尝试将数据推送到listview时它似乎不起作用。这是代码:

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 个答案:

答案 0 :(得分: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();
                    }
}

每当你获得数据时,你应该告诉它“嘿,我有一个新数据,所以让我们通知然后显示它”

答案 1 :(得分:1)

将数据添加到列表后,您必须在适配器上调用notifyDataSetChanged()