0

Its giving me "RecyclerView: No adapter attached; skipping layout" error but in my opinion adapter is correctly attached. Please help.

    public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private RecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        layoutManager = new LinearLayoutManager(MainActivity.this);
        recyclerView.setLayoutManager(layoutManager);
        requestJsonObject();
    }

    private void requestJsonObject(){
        RequestQueue queue = Volley.newRequestQueue(this);
        String url ="https://api.myjson.com/bins/2t4j3";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Response " + response);
                GsonBuilder builder = new GsonBuilder();
                Gson mGson = builder.create();
                List<ItemObject> posts = new ArrayList<ItemObject>();
                posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class));
                adapter = new RecyclerViewAdapter(MainActivity.this, posts);
                recyclerView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "Error " + error.getMessage());
            }
        });
        queue.add(stringRequest);
    }
}

I was moving my methods, but its not working. I was looking here for answer but there was only problems with wrong implemented adapters. I dont't know whats wrong here.

Tom
  • 57
  • 2

2 Answers2

0

Try to put this line:

adapter = new RecyclerViewAdapter(MainActivity.this, posts);

After adding a LayoutManager to the RecyclerView. Of course the list will be empty, but just init RecyclerAdapter and assign it to the RecyclerView

Then, when Volley completes it's request, use:

recyclerView.getAdapter().addAll(posts);
recyclerView.getAdapter().notifyDataSetChaged();

The last commands add elements to the RecyclerView's adapter and notify LayoutManager of the change.

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
0

You are calling setAdapter in a delay thread. When the view is created in the main thread, the Recyclerview do not have an adapter.

Put this two lines before setLayoutManager

List<ItemObject> posts = new ArrayList<ItemObject>();
adapter = new RecyclerViewAdapter(MainActivity.this, posts);

In onResponse, you can ethier set a new adpater or update the data in the adapter which depends on your implementation.

Joshua
  • 5,901
  • 2
  • 32
  • 52
  • I'm sorry but its not working :/ I was moving adapter in a few different places like before/after layout manager but its not working – Tom Jul 07 '16 at 10:33
  • i setAdapter before layoutManager and error dissapear but app is not working, its only showing white space and log cat is clear :/ https://scontent-fra3-1.xx.fbcdn.net/v/t34.0-12/13624505_1107401415965387_789590380_n.png?oh=a78e6331d61e135edd4cd9c757b85698&oe=577F9CFC – Tom Jul 07 '16 at 10:44
  • You should either keep the `setAdapter` in onResponse (This means TWO calls) or update your list im the adapter and call notifyDataSetChaged()`` – Joshua Jul 07 '16 at 10:48