Я использую несколько запросов для заполнения ReceyclerView, однако продолжаю получать сообщение об ошибке E/RecyclerView: No adapter attached; skipping layout
Проблема не в том, что я не вызываю RecyclerView
в OnCreate
, поскольку весь этот код находится в OnCreate
Я уже проверил, что мой RecyclerView
является моим OnCreate
, поэтому это не так.
Обновление: OnCreate
:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment, container, false);
final List<String> followingList = new ArrayList<>();
final List<Post> postList = new ArrayList<>();
final FollowerAdapter followerAdapter = new FollowerAdapter(postList);
final RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity(), RecyclerView.VERTICAL, false));
final LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
recyclerView.setAdapter(followerAdapter);
Query first = userFollowingReference;//TODO Realtime updates
first.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull final Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
String id = document.getId();
followingList.add(id);
}
followerAdapter.notifyDataSetChanged();
if(!followingList.isEmpty()) {
Query second = rootRef.collection("posts/" + followingList.get(0) + "/userPosts")
.orderBy("date", Query.Direction.ASCENDING)
.limit(3);
second.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull final Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
for(DocumentSnapshot document : task.getResult()){
Post post = document.toObject(Post.class);
postList.add(post);
}
followerAdapter.notifyDataSetChanged();
if(!postList.isEmpty()) {
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
@Override
public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
isScrolling = false;
Query third = rootRef.collection("posts/" + followingList.get(task.getResult().size() - 1) + "/userPosts")
.orderBy("date", Query.Direction.ASCENDING)
.startAfter(lastVisible).limit(3);
third.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> t) {
if (t.isSuccessful()) {
for (DocumentSnapshot d : t.getResult()) {
Post post = d.toObject(Post.class);
postList.add(post);
}
followerAdapter.notifyDataSetChanged();
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
if (t.getResult().size() < 3) {
isLastItemReached = true;
}
}
}
});
}
}
};
recyclerView.addOnScrollListener(onScrollListener);
}
}
}
});
}
}
}
});
return rootView;
}
Адаптер:
class FollowerAdapter extends RecyclerView.Adapter<PostViewHolder> {
private List<Post> list;
FollowerAdapter(List<Post> list) {
this.list = list;
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout, parent, false);
return new PostViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
Post post = list.get(position);
holder.setPost(post);
}
@Override
public int getItemCount() {
return list.size();
}
}
Когда я запускаю приложение, RecyclerView
пусто, и я получаю ошибку:
E/RecyclerView: No adapter attached; skipping layout
Также в процессе отладки я обнаружил, что followingList
выполняетсяправильно заполнен как list
, который содержит сообщения.Выглядит так, как будто RecyclerView
полностью пропущен.
Редактировать: Вот мой макет Firestore, если это полезно
Firestore-root
|
--- users (collection)
| |
| --- uid (documents)
| |
| --- name: "User Name"
| |
| --- email: "email@email.com"
|
--- following (collection)
| |
| --- uid (document)
| |
| --- userFollowing (collection)
| |
| --- uid (documents)
| |
| --- uid (documents)
|
--- posts (collection)
|
--- uid (documents)
|
--- userPosts (collection)
|
--- postId (documents)
|
--- postId (documents)
Спасибо, что покавсем помогите!
Обновление: Теперь я могу видеть несколько сообщений, но после прокрутки до нижней части RecyclerView
я получаю java.lang.IndexOutOfBoundsException
Сильфон - мой Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android,
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.android.Fragments.FollowingFragment$1$1$1.onScrolled(FollowingFragment.java:104)
at androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:5077)
at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5241)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)