未在RecyclerView Android中加载的项目

时间:2018-05-12 18:21:38

标签: android android-recyclerview

即使收到服务器的响应,RecyclerView也不会填充。

for (int i = 0; i < 30; i++) {
            VideoDetails videoDetails = new VideoDetails("Video " + i, "Video ID " + i, "This is video desc" + i);
            videoDetailsList.add(videoDetails);
        }
        mAdapter.notifyDataSetChanged();

如果我使用此代码(上图),则会填充项目&amp;显示在屏幕上。

我调试并检查我从服务器&amp;获得了正确的响应videoDetailsArrayList也会被填充,但这些项目不会显示在屏幕上。

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

        findViewByIds();
        videoDetailsArrayList = new ArrayList<>();

        mAdapter = new RecyclerViewAdapter(videoDetailsList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        prepareVideoData();
    }

    private void prepareVideoData() {
        /*for (int i = 0; i < 30; i++) { //**THIS CODE IN COMMENT WORKS**
            VideoDetails videoDetails = new VideoDetails("Video " + i, "Video ID " + i, "This is video desc" + i);
            videoDetailsList.add(videoDetails);
        }
        mAdapter.notifyDataSetChanged();*/

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("items");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        JSONObject jsonVideoId = jsonObject1.getJSONObject("id");
                        JSONObject jsonsnippet = jsonObject1.getJSONObject("snippet");
                        JSONObject jsonObjectdefault = jsonsnippet.getJSONObject("thumbnails").getJSONObject("medium");

                        String videoName = jsonsnippet.getString("title");
                        String videoId = jsonVideoId.getString("videoId");
                        String videoDesc = jsonsnippet.getString("description");

                        VideoDetails videoDetails = new VideoDetails(videoName, videoId, videoDesc);
                        //VideoDetails videoDetails1 = new VideoDetails("Video " + i, "Video ID " + i, "This is video desc" + i);
                        videoDetailsArrayList.add(videoDetails);
                    }
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }

0 个答案:

没有答案