-1

My recyclerview is not populating inside fragment. I am getting proper response from volley. I have parsed the JSON in arraylist in proper format only problem is that recyclerview is not populating.

MyFragment.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_undekha_tadka, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        myVolleyOperation(view);
        return view;
    }

    private void myVolleyOperation(View v) {
        volleySingleton = VolleySingleton.getInstance();
        requestQueue = volleySingleton.getRequestQueue();
        String url = "http://abcd.com/undekha.html";

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                jsonParsing(response);
                Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), "ERROR\n" + error.toString(), Toast.LENGTH_SHORT).show();
            }
        });

        requestQueue.add(stringRequest);

        recyclerViewAdapter = new RecyclerViewAdapter(undekhaList, v.getContext());
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(recyclerViewAdapter);
    }

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
    ArrayList<UndekhaModel> myArrayList = new ArrayList<>();
    Context context;

    VolleySingleton volleySingleton;
    ImageLoader imageLoader;

    public RecyclerViewAdapter(ArrayList<UndekhaModel> myArrayList, Context context) {
        this.myArrayList = myArrayList;
        this.context = context;

        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton.getImageLoader();
    }

    @Override
    public RecyclerViewAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row, parent, false);
        RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, context, myArrayList);

        return recyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewAdapter.RecyclerViewHolder holder, int position) {
        UndekhaModel undekhaModel = myArrayList.get(position);

        holder.title.setText(undekhaModel.getTitle());

        String loadUrl = "http://img.youtube.com/vi/" + undekhaModel.getId() + "/0.jpg";
        if (loadUrl!=null){
            imageLoader.get(loadUrl, new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                    holder.thumbnail.setImageBitmap(response.getBitmap());
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    holder.thumbnail.setImageResource(R.mipmap.ic_launcher);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return myArrayList.size();
    }

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        ImageView thumbnail;
        ArrayList<UndekhaModel> modelArrayList = new ArrayList<>();
        Context context;

        public RecyclerViewHolder(View itemView, final Context ctx, final ArrayList<UndekhaModel> modelArrayList) {
            super(itemView);
            this.context = ctx;
            this.modelArrayList = modelArrayList;

            title = (TextView) itemView.findViewById(R.id.tvTitle);
            thumbnail = (ImageView) itemView.findViewById(R.id.ivShow);
        }
    }
}
akkk
  • 1,457
  • 4
  • 23
  • 41

2 Answers2

0

Your method jsonParsing() needs to populate data for your RecyclerViewAdapter, needs to notify of the update and the adapter needs to return the correct count of items it is managing. Right now its getItemCount() is always returning 0, so the RecyclerView does not think there is anything to show.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • You'll need to provide more code (like the `jsonParsing()` routine) and how the data in the adapter is updated. – Larry Schiefer Oct 14 '16 at 16:45
  • `jsonParsing()` is working fine. I checked it, so I thought it will be useless to post `jsonParsing()` code – akkk Oct 14 '16 at 17:16
  • It may be parsing the json fine, but what does it actually do with the data? That would be the reason for posting the code. Right now your adapter is never updated with data. – Larry Schiefer Oct 14 '16 at 18:21
0

Same issue than here

https://stackoverflow.com/a/35802948/2144418

Try setting the adapter before the layout manager

Change the last two lines on myVolleyOperation to this

recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setLayoutManager(layoutManager);
Community
  • 1
  • 1
Aerim
  • 1,960
  • 3
  • 16
  • 28
  • Can you try putting a breakpoint on the adapter's **getItemCount** and see what it's returning? – Aerim Oct 14 '16 at 17:41