填充RecyclerView列表

时间:2018-09-10 09:51:43

标签: java android android-recyclerview android-adapter

我似乎无法完全填充RecyclerView。它仅使第一个单元格,其他单元格为空白。即使for循环似乎多次运行。

    JsonObjectRequest root = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject feed = response.getJSONObject("feed");
                JSONArray results = feed.getJSONArray("results");
                //Log.d("FEED", String.valueOf(feed));w
                for(int i = 0; i<results.length(); i++){
                    JSONObject resultsObj = results.getJSONObject(i);

                    Log.d(TAG, "inside" + String.valueOf(i));
                    mImageUrls.add(resultsObj.getString("artworkUrl100"));
                    mTitle.add(resultsObj.getString("name"));
                    getRecyclerView();
                }

                adapter.notifyDataSetChanged();

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    mRequestQueue.add(root);

}

private void getRecyclerView(){
    Log.d(TAG, "getRecyclerView");
    recyclerView = findViewById(R.id.recyclerView);
    adapter = new RecyclerViewAdapter(mImageUrls, mTitle, this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
}

3 个答案:

答案 0 :(得分:0)

告诉我这是否可行:

JsonObjectRequest root = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject feed = response.getJSONObject("feed");
                JSONArray results = feed.getJSONArray("results");
                //Log.d("FEED", String.valueOf(feed));w
                for(int i = 0; i<results.length(); i++){
                    JSONObject resultsObj = results.getJSONObject(i);

                    Log.d(TAG, "inside" + String.valueOf(i));
                    mImageUrls.add(resultsObj.getString("artworkUrl100"));
                    mTitle.add(resultsObj.getString("name"));
                }

                getRecyclerView();
                adapter.notifyDataSetChanged();

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    mRequestQueue.add(root);

}

private void getRecyclerView(){
    Log.d(TAG, "getRecyclerView");
    recyclerView = findViewById(R.id.recyclerView);
    adapter = new RecyclerViewAdapter(mImageUrls, mTitle, this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true); }

我所做的是将getRecyclerView();移出循环,因为您只需要调用一次即可,而不必每次都想设置recyclerview。

答案 1 :(得分:0)

每次用一个值初始化适配器。这就是列表包含一个值而其他值为空白的原因。您需要将Create Materialized view temp_mv refresh complete start with (sysdate) next (TRUNC(add_months(SYSDATE,1),'MM')) as select ... 移动到循环之外。

看起来像这样:

getRecyclerView();

答案 2 :(得分:0)

使用它可能对您有帮助。

private void getRecyclerView(){
    Log.d(TAG, "getRecyclerView");
    recyclerView = findViewById(R.id.recyclerView);
    adapter = new RecyclerViewAdapter(mImageUrls, mTitle, this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, 
    LinearLayoutManager.VERTICAL, false));
    recyclerView.setHasFixedSize(true);
}