0

I believe through my findings is that the issue could be with getActivity(), but I keep getting the same error over and over again.

I think this is all coming from using activities and fragments. If anybody has any advice regarding this issue, I would really appreciate the help!

// Recycler View
private DatabaseReference mDatabase;
private FirebaseUser mUser;
private List<UserHabits> mUserHabitsList;
private RecyclerView mRecyclerView;
private String uID;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab1_habits_fragment, container,false);

    // Recycler View
    mRecyclerView = view.findViewById(R.id.recycler_view_habits);
    //LinearLayoutManager manager = new LinearLayoutManager(getActivity());
    mRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(container.getContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    prepareUserHabitsData();


    mUser = FirebaseAuth.getInstance().getCurrentUser();
    if (mUser != null) {
        uID = mUser.getUid();
        Log.i("USER ID", uID);
    }

    mDatabase = FirebaseDatabase.getInstance().getReference("users");

    return view;
}

private void prepareUserHabitsData() {
    new GetDataFromFirebase().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    // Read From Database
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("users");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            mUserHabitsList = new ArrayList<>();

            for (DataSnapshot habitsSnapshot: dataSnapshot.getChildren()) {
                String habitName = (String) habitsSnapshot.child("GbovxRp2QrW4wV1KSI8nfTjJwpW2").child("Test").getValue();

                UserHabits userHabits = new UserHabits(habitName);
                mUserHabitsList.add(userHabits);
            }
            mRecyclerView.setAdapter(new HabitsAdapter(mUserHabitsList));
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.i("ERROR", "FAILED TO READ VALUE");
        }
    });
}
  • Check this out https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout – iCantC May 17 '20 at 18:12
  • You are trying to show a recyclerview before it has an adapter. The Firebase query doesn't happen immediately, so you are attaching the adapter late. You should either attach an adapter immediately and update it later when the query finishes, or don't display the view until you have an adapter with data. – Doug Stevenson May 17 '20 at 18:15

0 Answers0