Android Recyclerview无适配器连接跳过布局片段
我在片段中使用了firestore和回收视图。但是由于这个错误,我感到非常沮丧。Android Recyclerview无适配器连接跳过布局片段,android,android-recyclerview,Android,Android Recyclerview,我在片段中使用了firestore和回收视图。但是由于这个错误,我感到非常沮丧。Recyclerview也不会显示。当我们打开“活动”时,它显示为空白,活动上没有任何内容。(这类问题已经被问过了,但与我的问题有很多不同,比如我的代码非常不同,在这个问题上,他能够看到Recyclerview,而我没有。) 公共类ForthFragment扩展片段{ 查看我的视图; 私人回收站; 私人FirebaseFirestore mFirestore; 私有静态最终字符串TAG=“FireLog”; 私有列表
Recyclerview
也不会显示。当我们打开“活动”时,它显示为空白,活动上没有任何内容。(这类问题已经被问过了,但与我的问题有很多不同,比如我的代码非常不同,在这个问题上,他能够看到Recyclerview
,而我没有。)
公共类ForthFragment扩展片段{
查看我的视图;
私人回收站;
私人FirebaseFirestore mFirestore;
私有静态最终字符串TAG=“FireLog”;
私有列表用户列表;
私有用户slistadapter用户slistadapter;
私有上下文应用上下文;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
myView=充气机。充气(R.layout.fragment_forth,容器,错误);
mMainList=(RecyclerView)myView.findViewById(R.id.mainlist);
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(新的LinearLayoutManager(this.getApplicationContext());
setAdapter(usersListAdapter);
onconnected();
LinearLayoutManager llm=新的LinearLayoutManager(this.getApplicationContext());
mFirestore=FirebaseFirestore.getInstance();
usersList=newarraylist();
usersListAdapter=新的usersListAdapter(usersList);
mFirestore.collection(“Users”).addSnapshotListener(新的EventListener(){
@凌驾
public void OneEvent(QuerySnapshot文档快照,FireBaseFireStore异常e){
如果(e!=null){
Log.d(标记“Error:+e.getMessage());
}
对于(DocumentChange文档:documentSnapshots.getDocumentChanges()){
if(doc.getType()==DocumentChange.Type.ADDED){
Users Users=doc.getDocument().toObject(Users.class);
usersList.add(用户);
usersListAdapter.notifyDataSetChanged();
}
}
}
});
返回myView;
}
私有无效OnConnected(){
LinearLayoutManager llm=新的LinearLayoutManager(this.getActivity());
llm.设置方向(线性布局管理器.垂直);
mMainList.setLayoutManager(llm);
setAdapter(usersListAdapter);
}
公共上下文getApplicationContext(){
返回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()));
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;
}
@覆盖
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
myView=充气机。充气(R.layout.fragment_forth,容器,错误);
mMainList=(RecyclerView)myView.findViewById(R.id.mainlist);
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(新的LinearLayoutManager(this.getApplicationContext());
onconnected();
mFirestore=FirebaseFirestore.getInstance();
usersList=newarraylist();
mFirestore.collection(“Users”).addSnapshotListener(新的EventListener(){
@凌驾
public void OneEvent(QuerySnapshot文档快照,FireBaseFireStore异常e){
如果(e!=null){
Log.d(标记“Error:+e.getMessage());
}
对于(DocumentChange文档:documentSnapshots.getDocumentChanges()){
if(doc.getType()==DocumentChange.Type.ADDED){
Users Users=doc.getDocument().toObject(Users.class);
usersList.add(用户);
usersListAdapter.notifyDataSetChanged();
}
}
}
});
返回myView;
}
私有无效OnConnected(){
LinearLayoutManager llm=新的LinearLayoutManager(this.getActivity());
llm.设置方向(线性布局管理器.垂直);
mMainList.setLayoutManager(llm);
usersListAdapter=新的usersListAdapter(usersList);
setAdapter(usersListAdapter);
}
公共上下文getApplicationContext(){
返回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()));
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;
}