Android凌空响应数据未加载到视图

时间:2016-11-21 19:35:24

标签: android android-recyclerview android-volley recycler-adapter

我遇到了一个奇怪的问题,我使用排球获取GET响应并将数据加载到回收器视图;但是,除非我在应用程序运行时锁定并解锁手机,否则数据不会反映在视野中。然后我尝试了相同的响应本地存储到资产文件夹,其工作超精细。我没有得到任何线索,GET响应显示在记录器中;我也尝试过和Listview一样,但结果总是一样的。请帮助我......

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

    newsFeed = engine();

    rv = (RecyclerView) findViewById(R.id.newsItems);
    rv.setHasFixedSize(true);
    llm = new LinearLayoutManager(this);
    rv.setLayoutManager(llm);

    initializeAdapter(newsFeed);



}


private void initializeAdapter(ArrayList<newsItem> newsFeed) {
    context = getApplicationContext();
    NewsViewAdapter adapter = new NewsViewAdapter(context, newsFeed);
    rv.setAdapter(adapter);
}


private ArrayList<newsItem> engine() {
    //String res=loadJSONFromAsset();
    RequestQueue queue = Volley.newRequestQueue(this);
    final ArrayList<newsItem> reqs = new ArrayList<>();
    JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
            "http://content.guardianapis.com/search?page=20&q=business%20OR%20sport&show-fields=thumbnail&api-key={mykey}",
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        //  longInfo(response.toString());
                        JSONObject newsItems = response.getJSONObject("response");
                        JSONArray webNews = newsItems.getJSONArray("results");
                        int a = webNews.length();

                        for (int i = 0; i < a; i++) {
                            JSONObject temp = webNews.getJSONObject(i);
                            String title = temp.getString("webTitle");
                            Log.d("mytitile", title);
                            // String time = temp.getString("time");
                            String date = temp.getString("webPublicationDate");
                            String content = temp.getString("id");
                            Log.d("myContent", content);
                            String link = temp.getString("webUrl");
                            JSONObject temp_img = temp.getJSONObject("fields");
                            String image = temp_img.getString("thumbnail");
                            Log.d("imageUrl", image);
                            reqs.add(new newsItem(title, content, date, link, image));

                        }
                    } catch (JSONException e) {
                        Log.i("myTag", e.toString());
                    }
                }

            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("myTag", error.toString());
                }
            });


    myReq.setRetryPolicy(new DefaultRetryPolicy(
            10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
    ));
    queue.add(myReq);

2 个答案:

答案 0 :(得分:3)

使用此:

在您的字段中:

private ArrayList<newsItem> items = new ArrayList<>();
private NewsViewAdapter adapter;
你的initializeAdapter中的

 adapter = new NewsViewAdapter(context, items);

并在你的onResponse中,而不是reqs.add

 items.add(new newsItem(parameters..));

在onResponse中进行for循环后,调用

 adapter.notifyDataSetChanged();

你很高兴

答案 1 :(得分:1)

您的适配器不知道数据集已更改,直到您通过打开和关闭屏幕强制它再次检查。 (我假设如果您还旋转手机,数据也会更新。)

一种解决方案是通过调用notifyDataSetChanged()

通知您的适配器数据已手动更改

首先使NewsViewAdapter adapter为全局变量:

adapter.notifyDataSetChanged();
for

onResponse()循环后