0

I am doing a project. But I am getting error repeatedly for 3 days long and can't solve it. This is my main activity:

AlertDialog dialog;
IFirebaseLoadListener iFirebaseLoadListener;
RecyclerView my_recyler_view;
DatabaseReference mydata;

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

    //init

    mydata= FirebaseDatabase.getInstance().getReference("MyData");
    dialog=new SpotsDialog.Builder().setContext(this).build();
    iFirebaseLoadListener=this;

    //View

    my_recyler_view =findViewById(R.id.my_recyclr_view);
    my_recyler_view.setHasFixedSize(true);
    my_recyler_view.setLayoutManager(new LinearLayoutManager(this));


    //Load Data

    getFirebaseData();
}

private void getFirebaseData() {

    dialog.show();

    mydata.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            List<ItemGroup> itemGroups=new ArrayList<>();
            for (DataSnapshot groupSnapShot:dataSnapshot.getChildren()) {

                ItemGroup itemGroup = new ItemGroup();
                itemGroup.setHeaderTitle(groupSnapShot.child("headerTitle").getValue(true).toString());
                GenericTypeIndicator<ArrayList<ItemData>> genericTypeIndicator = new GenericTypeIndicator<ArrayList<ItemData>>() {
                    @Override
                    public int hashCode() {
                        return super.hashCode();
                    }
                };

                itemGroup.setListItem(groupSnapShot.child("listItem").getValue(genericTypeIndicator));

                itemGroups.add(itemGroup);
            }
            iFirebaseLoadListener.onFirebaseLoadSuccess(itemGroups);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

            iFirebaseLoadListener.onFirebaseLocationFailed(databaseError.getMessage());

        }
    });
}

@Override
public void onFirebaseLoadSuccess(List<ItemGroup> itemGroupList) {

    MyItemGroupAdapter adapter=new MyItemGroupAdapter(this,itemGroupList);
    my_recyler_view.setAdapter(adapter);

    dialog.dismiss();

}

@Override
public void onFirebaseLocationFailed(String message) {

    Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
    dialog.dismiss();

}

}

Here is my stack trace

2019-10-13 11:56:39.671 1870-1880/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property

2019-10-13 11:56:39.705 1870-1870/? D/SurfaceFlinger: duplicate layer name: changing com.example.mainactivity/com.example.mainactivity.MainActivity to com.example.mainactivity/com.example.mainactivity.MainActivity#1

2019-10-13 11:56:39.710 1597-1643/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496

2019-10-13 11:56:39.720 1597-1597/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496

2019-10-13 11:56:39.736 1597-1643/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496

2019-10-13 11:56:39.776 7854-7854/com.example.mainactivity E/RecyclerView: No adapter attached; skipping layout

touhid udoy
  • 4,005
  • 2
  • 18
  • 31
  • Possible duplicate of [E/RecyclerView: No adapter attached; skipping layout -- when making a todo list app](https://stackoverflow.com/questions/58034951/e-recyclerview-no-adapter-attached-skipping-layout-when-making-a-todo-list) – Sammy T Oct 13 '19 at 06:07
  • But how can i correct my error from your previous answer with this specific code? please help on this, I have stuck three days on this. – Asif Hossain Oct 13 '19 at 06:27

2 Answers2

1

You should create and set the Adapter inside onCreate() and when data updates, let the adapter know that data has been updated using yourAdaper.notifyDataSetChanged() method from the onFirebaseLoadSuccess() method

touhid udoy
  • 4,005
  • 2
  • 18
  • 31
1

Basically, what you're missing is setting the adapter to the recyclerView in onCreate().

  • First, declare this two as global variables before onCreate():

    List<ItemGroup> itemGroups; MyItemGroupAdapter adapter;

  • then, in the onCreate(), assign them:

    itemGroups=new ArrayList<>();
    adapter=new MyItemGroupAdapter(this,itemGroupList);
    my_recyler_view.setAdapter(adapter);
    
  • Then Update the itemGroups as you're doing.

  • After that, in the onFirebaseLoadSuccess(), simply update the data as: adapter.updateData(myNewData); adapter.notifyDataSetChanged();
Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50