简体   繁体   English

Android Volley响应数据未加载以查看

[英]Android volley response data not loading to view

Am experiencing a strange problem here, I got the GET response using volley and loading the data to the recycler view; 这里遇到一个奇怪的问题,我使用截击得到了GET响应,并将数据加载到了回收站视图; the data however not reflecting in view unless I lock and unlock the phone once , while the application is running. 但是,除非在应用程序运行时锁定和解锁手机一次 ,否则数据不会在视图中反映出来。 Then I tried the same response locally storing into asset folder,its working superfine. 然后我尝试将相同的响应本地存储到资产文件夹中,它的工作原理非常好。 Am not getting any clue on this, the GET responses are showing in the logger; 没有任何线索,GET响应显示在记录器中; I've tried the same with Listview too, but the result was same always. 我也尝试过用Listview进行同样的操作,但是结果总是一样。 Please help me on this.... 请帮助我。

 @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);

Use this: 用这个:

in your fields: 在您的字段中:

private ArrayList<newsItem> items = new ArrayList<>();
private NewsViewAdapter adapter;

in your initializeAdapter: 在您的initializeAdapter中:

 adapter = new NewsViewAdapter(context, items);

and in your onResponse, instead of reqs.add 并在您的onResponse中,而不是reqs.add中

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

after your for loop in the onResponse, you call 在onResponse中的for循环之后,您调用

 adapter.notifyDataSetChanged();

and you're good to go 而且你很好走

Your Adapter is unaware that the data set has changed, until you force it to check again by turning the screen on and off. 您的适配器不知道数据集已更改,直到您通过打开和关闭屏幕强制其再次检查。 (I'm assuming the data will update if you rotate your phone as well.) (我假设如果同时旋转手机,数据也会更新。)

One solution is to notify your Adapter that the data has changed manually by calling notifyDataSetChanged() 一种解决方案是通过调用notifyDataSetChanged()来通知适配器数据已手动更改。

First make NewsViewAdapter adapter a global variable then: 首先将NewsViewAdapter adapter为全局变量,然后:

adapter.notifyDataSetChanged();

after you for loop in onResponse() for循环中onResponse()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM