1

Just trying to show in a RecyclerView the info from my database. everything works fine. The objects are displayed.

but logcat says:

E/RecyclerView: No adapter attached; skipping layout

This is my the code (DisplayImages.java)

    public class DisplayImages extends AppCompatActivity {
    DatabaseReference databaseReference;

    RecyclerView recyclerView;

    RecyclerView.Adapter adapter;
    ProgressDialog progressDialog;

    List<ImageUploadInfo> list = new ArrayList<>();

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

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));

        progressDialog = new ProgressDialog(DisplayImages.this);
        progressDialog.setMessage("Loading Images From Firebase.");
        progressDialog.show();

        // Setting up Firebase image upload folder path in databaseReference.
        // The path is already defined in MainActivity.
        databaseReference = FirebaseDatabase.getInstance().getReference(Main.Database_Path);

        databaseReference.addValueEventListener(new ValueEventListener() {
            @override
            public void onDataChange(DataSnapshot snapshot) {

                for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                    ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);

                    list.add(imageUploadInfo);
                }

                adapter = new RecyclerViewAdapter(getApplicationContext(), list);

                recyclerView.setAdapter(adapter);
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

                progressDialog.dismiss();

            }
        });

    }

And If it makes any difference, this is what i wrote in MainActivity about the DataBase_Path :

public class Main extends AppCompatActivity implements View.OnClickListener {

DatabaseReference databaseReference;
public static final String Database_Path = "All_Image_Uploads_Database";

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

    databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);

As you can see I have attached an adapter for Recycleview. so why do I keep getting this error?

i have read other questions related to same problem but none helps.

Anne
  • 13
  • 4
  • 1
    Possible duplicate of [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – Chisko Mar 09 '18 at 00:19
  • To get of the warnings move the set adapter portion above the async method and update the data of the adapter in the async method only – Md Johirul Islam Mar 09 '18 at 00:26
  • @Anne as the above comment said you should move the creation of the adapter to right after you're creating the layout. You can then set an empty list and when you're firebase comes back you can update the list and notifyDatasetChanged() on the adapter. I've edited my answer to better explain :) – Levi Moreira Mar 09 '18 at 00:29

2 Answers2

0

I don't see anywhere you are creating an adapter and setting that adapter to RecyclerView. You need to create an adapter and set adapter like following:

mAppAdapter = new AppAdapter(mModel); // or whatever constructor you want
recyclerView.setAdapter(mAppAdapter);

EDIT:

You are setting adapter to RecyclerView inside an asynchronous method. So your program will have a non deterministinc amount of time where the adapter is not actually set to recyclerView. To get rid off the warning you get you should initialize and set the adapter above the async method and only update data of the adapter inside the async method

Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
0

Yyou need to set the adapter after you set the layour manager, otherwise you'll get the error message you're seeing.

recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
//don't set adapter here
    recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));

//set adapter here

Moreover, you can set the adapter with an empty list first and then when you receive the callback from firebase you can update the list and call notifyDatasetChanged() on the adapter. The problem you see is due to the fact that you're not actually setting the adapter until very later in the process (when your firebase call comes back)

Like this:

recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));
adapter = new RecyclerViewAdapter(getApplicationContext(), list);
 recyclerView.setAdapter(adapter);


databaseReference.addValueEventListener(new ValueEventListener() {
            @override
            public void onDataChange(DataSnapshot snapshot) {

                for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                    ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);

                    list.add(imageUploadInfo);
                }
               adapter.notifyDatasetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

                progressDialog.dismiss();

            }
        });
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • I did not understand what I should change in the following line: 'RecyclerView.Adapter adapter;' . I want to display an Imageview and a textview – Anne Mar 09 '18 at 00:30
  • sorry Anne, that was before I saw your complete code. I reckon if you just move the creation to before the firebase call the error will likely disappear. – Levi Moreira Mar 09 '18 at 00:32
  • The inf from the firease doesnt displayed , but at least the error disappeared!!!!! Thank you !!♥ – Anne Mar 09 '18 at 00:49
  • glad it helped :) – Levi Moreira Mar 09 '18 at 00:51