RecyclerView:没有附加适配器跳过布局(TabbedActivity + Firestore)
RecyclerView: No adapter attached skipping layout (TabbedActivity + Firestore)
我正在尝试从 firestore 检索数据并将其显示为 TabbedActivity 中的 RecyclerView。数据显示在每个 tab/fragment 中,但有时如果我在选项卡中向前滑动然后向后滑动,数据就会消失,并出现错误 "No adapter attached skipping layout"。
Fragment.java
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.sligo_fragment, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.sligo_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db.collection("sligo")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult()) {
placeNames.add(document.getData().get("Name").toString());
}
MyAdapter adapter = new MyAdapter(placeNames);
recyclerView.setAdapter(adapter);
}
});
}
MyAdapter.java
public static class MyViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.info_text);
}
}
public MyAdapter(ArrayList<String> myDataset) {
mDataset = myDataset;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
根据 Fragment LifeCycle onCreate
在 onCreateView
之前调用。除此之外,您正在异步操作中设置适配器,这需要时间才能完成。尝试将代码移动到 onCreateView
,如下所示:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.sligo_fragment, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.sligo_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
MyAdapter adapter = new MyAdapter(placeNames);
recyclerView.setAdapter(adapter);
db.collection("sligo")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult()) {
placeNames.add(document.getData().get("Name").toString());
}
adapter.notifyDataSetChanged();
}
});
return v;
}
我正在尝试从 firestore 检索数据并将其显示为 TabbedActivity 中的 RecyclerView。数据显示在每个 tab/fragment 中,但有时如果我在选项卡中向前滑动然后向后滑动,数据就会消失,并出现错误 "No adapter attached skipping layout"。
Fragment.java
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.sligo_fragment, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.sligo_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db.collection("sligo")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult()) {
placeNames.add(document.getData().get("Name").toString());
}
MyAdapter adapter = new MyAdapter(placeNames);
recyclerView.setAdapter(adapter);
}
});
}
MyAdapter.java
public static class MyViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.info_text);
}
}
public MyAdapter(ArrayList<String> myDataset) {
mDataset = myDataset;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
根据 Fragment LifeCycle onCreate
在 onCreateView
之前调用。除此之外,您正在异步操作中设置适配器,这需要时间才能完成。尝试将代码移动到 onCreateView
,如下所示:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.sligo_fragment, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.sligo_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
MyAdapter adapter = new MyAdapter(placeNames);
recyclerView.setAdapter(adapter);
db.collection("sligo")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult()) {
placeNames.add(document.getData().get("Name").toString());
}
adapter.notifyDataSetChanged();
}
});
return v;
}