-2

I have used firestore with Recyclerview, in a Fragment. but I am getting very frustrated because of this error. the Recyclerview is not displayed as well. when we open the activity it shows blank there is nothing on the activity. (this type of question was already asked but with that and my question there are a lot of difference like my code is very different and in that question he is able to see the Recyclerview whereas I am not.)

public class ForthFragment extends Fragment {

View myView;
private RecyclerView mMainList;
private FirebaseFirestore mFirestore;
private static final  String TAG = "FireLog";
private List<Users> usersList;
private UsersListAdapter usersListAdapter;
private Context applicationContext;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.fragment_forth, container, false);

    mMainList = (RecyclerView) myView.findViewById(R.id.mainlist);
    mMainList.setHasFixedSize(true);
    mMainList.setLayoutManager(new LinearLayoutManager(this.getApplicationContext()));
    mMainList.setAdapter(usersListAdapter);
    onConencted();

    LinearLayoutManager llm = new LinearLayoutManager(this.getApplicationContext());

    mFirestore = FirebaseFirestore.getInstance();
    usersList = new ArrayList<>();
    usersListAdapter = new UsersListAdapter(usersList);

    mFirestore.collection("Users").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

            if (e != null) {

                Log.d(TAG, "Error : " + e.getMessage());

            }
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                if (doc.getType() == DocumentChange.Type.ADDED) {

                    Users users = doc.getDocument().toObject(Users.class);
                    usersList.add(users);
                    usersListAdapter.notifyDataSetChanged();


                }

            }

        }
    });


    return myView;
}

private void onConencted() {
    LinearLayoutManager llm = new LinearLayoutManager(this.getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    mMainList.setLayoutManager(llm);
    mMainList.setAdapter( usersListAdapter );
}


public Context getApplicationContext() {
    return applicationContext;
}

}

ADM
  • 20,406
  • 11
  • 52
  • 83
Rohit
  • 1
  • 4
    Possible duplicate of [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – ADM Feb 17 '18 at 15:43

1 Answers1

0

Try with below code :

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.fragment_forth, container, false);

    mMainList = (RecyclerView) myView.findViewById(R.id.mainlist);
    mMainList.setHasFixedSize(true);
    mMainList.setLayoutManager(new LinearLayoutManager(this.getApplicationContext()));

    onConencted();



    mFirestore = FirebaseFirestore.getInstance();
    usersList = new ArrayList<>();


    mFirestore.collection("Users").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

            if (e != null) {

                Log.d(TAG, "Error : " + e.getMessage());

            }
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                if (doc.getType() == DocumentChange.Type.ADDED) {

                    Users users = doc.getDocument().toObject(Users.class);
                    usersList.add(users);
                    usersListAdapter.notifyDataSetChanged();


                }

            }

        }
    });


    return myView;
}

private void onConencted() {
    LinearLayoutManager llm = new LinearLayoutManager(this.getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    mMainList.setLayoutManager(llm);
     usersListAdapter = new UsersListAdapter(usersList);
    mMainList.setAdapter( usersListAdapter );
}


public Context getApplicationContext() {
    return applicationContext;
}
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44