我在片段中使用了Recyclerview
的firestore。但由于这个错误,我感到非常沮丧。 Recyclerview
也未显示。当我们打开活动时,它显示为空白,活动中没有任何内容。 (这种类型的问题已经被问到,但是我的问题有很多不同,比如我的代码非常不同,在那个问题中他能够看到Recyclerview
而我不是。)
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;
}
}
答案 0 :(得分:0)
尝试使用以下代码:
@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;
}