0

I am new to Android programming and I'm trying to create a cardview using recycler view using an API but I am getting the error saying "E/RecyclerView: No adapter attached; skipping layout". I tried looking up the solution online but couldn't seem to figure it out.

I tried the solution given by this link :

http://www.chansek.com/RecyclerView-no-adapter-attached-skipping-layout/

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;

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

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setHasFixedSize(true);

    mExampleList = new ArrayList<>();
    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}

private void parseJSON()
{
    String url = "http://apidev.travelhouse.world/api/v1/packages";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("");

                for (int i = 0; i < jsonArray.length(); i++)
                {
                    JSONObject hit = jsonArray.getJSONObject(i);

                    String holiday_name = hit.getString("holiday_name");
                    String holiday_price = hit.getString("package_price");
                    String primary_image = hit.getString("primary_image");

                    mExampleList.add(new ExampleItem(primary_image,holiday_name,holiday_price));

                }

                mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
                mRecyclerView.setAdapter(mExampleAdapter);

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


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

        }
    })

    {
        /** Passing some request headers* */
        @Override
        public Map getHeaders() throws AuthFailureError {
            HashMap headers = new HashMap();
            headers.put("Content-Type", "application/json");
            headers.put("X-API-KEY", "CODEX@123");
            return headers;
        }
    };

    mRequestQueue.add(request);
}

}

Imran
  • 84
  • 10
  • Possible duplicate of [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – shb Jul 17 '19 at 20:06
  • I tried that method but it didnt work either – Imran Jul 17 '19 at 20:41

3 Answers3

1

I think this is solution which Vlad Kramarenko recommended. I just implemented it in your code and I agree with it. So the problem is that you need to set Adapter for RecyclerView at the beginning. Once it is set up, you linked your List of ExampleItems to your RecyclerView through ExampleAdapter. Now every time you change your list and you want to updated your RecyclerView, you need to call notifyDataSetChanged() method on your Adapter which is linked to RecyclerView.

private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;

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

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setHasFixedSize(true);

    mExampleList = new ArrayList<>();
    mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
    mRecyclerView.setAdapter(mExampleAdapter);

    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}

private void parseJSON()
{
    String url = "http://apidev.travelhouse.world/api/v1/packages";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("");

                for (int i = 0; i < jsonArray.length(); i++)
                {
                    JSONObject hit = jsonArray.getJSONObject(i);

                    String holiday_name = hit.getString("holiday_name");
                    String holiday_price = hit.getString("package_price");
                    String primary_image = hit.getString("primary_image");

                    mExampleList.add(new ExampleItem(primary_image,holiday_name,holiday_price));

                }

                mRecyclerView.getAdapter().notifyDataSetChanged();

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


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

        }
    })

    {
        /** Passing some request headers* */
        @Override
        public Map getHeaders() throws AuthFailureError {
            HashMap headers = new HashMap();
            headers.put("Content-Type", "application/json");
            headers.put("X-API-KEY", "CODEX@123");
            return headers;
        }
    };

    mRequestQueue.add(request);
}
Ban Markovic
  • 690
  • 1
  • 7
  • 12
0

Maybe this will sound strange for you, but it says that you create your recyclerview but forgot to add the adapter to recyclerView... But don't forget, you do it. So why?

Answer: when the system tries to draw recyclerView it doesn't have an adapter. You attach adapter in parseJSON function after getting a response from the server. Request and respons from server take some time, but int the meantime system finishes drawing views and says: Hey, your recyclerView doesn't have an adapter, please fix it. You can ignore this warning, but if you want to make all things perfect: you can attach empty adapter in onCreate, and after getting a response do

mRecyclerView.getAdapter().notifyDataSetChanged();

EDIT: this how will look your onCreate:

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

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);

    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setHasFixedSize(true);

    mExampleList = new ArrayList<>();
    mRecyclerView.setAdapter(new ExampleAdapter(mExampleList, some arguments)

    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}
Vlad Kramarenko
  • 266
  • 3
  • 18
  • thanks for looking into it but it seems it still giving the same error. I created an empty adapter in the onCreate method. Could you please tell me exactly where the code you mentioned would fit into my code? I think I am placing it wrong. – Imran Jul 17 '19 at 19:56
  • He had asked you to set the adapter on recyclerView. Do this in onCreate not in parseJson function. mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList); mRecyclerView.setAdapter(mExampleAdapter); Then in parseJson function add items to the list and use notifyDatasetChanged. – Adarsh Anurag Jul 17 '19 at 19:59
  • Vlad could you tell me where to place "mRecyclerView.getAdapter().notifyDataSetChanged();" exactly in the parseJSON method? I made the onCreate method just like you mentioned. – Imran Jul 17 '19 at 20:12
  • put it in a place where you create an adapter later. Also don't forget to change list – Vlad Kramarenko Jul 17 '19 at 20:56
0

In the onCreate Method, add after creating array list

mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);

Remove them from the try clause

Kakaye
  • 11
  • 3