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;
}
}