未连接适配器;跳过布局_____ AndroidX RecyclerView



我正试图获得一个帖子列表显示在一个fragment RecyclerView在Android Studio使用Java我确实有"。setadapter ()"代码显示,但我仍然收到这个错误信息,我已经被困在这一个星期。我知道这很愚蠢这是我的片段,我的适配器,以及片段的XML文件

片段

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.zapster_picture_sharing.Adapter.PostAdapter;
import com.example.zapster_picture_sharing.Model.Post;
import com.example.zapster_picture_sharing.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
private PostAdapter postAdapter;
private List<Post> postLists;

private List<String> followingList;
ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
progressBar = view.findViewById(R.id.progress_circular);
checkFollowing();
recyclerView = view.findViewById(R.id.recycler_view_story);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
postLists = new ArrayList<>();
postAdapter = new PostAdapter(getContext(), postLists, true);
recyclerView.setAdapter(postAdapter);
return view;
}
private void checkFollowing(){
followingList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
followingList.clear();
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
followingList.add(snapshot.getKey());
}
readPosts();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readPosts(){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postLists.clear();
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
Post post = snapshot.getValue(Post.class);
for(String id:followingList){
if(post.getPublisher().equals(id)){
postLists.add(post);
}
}
}
postAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}

PostAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.zapster_picture_sharing.Model.Post;
import com.example.zapster_picture_sharing.Model.User;
import com.example.zapster_picture_sharing.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.List;
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
public Context mContext;
public List<Post> mPosts;
//private FirebaseUser firebaseUser;
public PostAdapter(Context mContext, List<Post> mPosts, boolean b){
this.mContext = mContext;
this.mPosts = mPosts;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.post_item, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
//firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final Post post = mPosts.get(i);
Glide.with(mContext).load(post.getPostimage()).into(viewHolder.post_image);
if (post.getDescription().equals("")){
viewHolder.description.setVisibility(View.GONE);
} else {
viewHolder.description.setVisibility(View.VISIBLE);
viewHolder.description.setText(post.getDescription());
}
publisherInfo(viewHolder.image_profile, viewHolder.username, viewHolder.publisher, post.getPublisher());
}
@Override
public int getItemCount() {
return mPosts.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView image_profile, post_image, like, comment, save;
public TextView username, likes, publisher, description, comments;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image_profile = itemView.findViewById(R.id.image_profile);
post_image = itemView.findViewById(R.id.post_image);
comments = itemView.findViewById(R.id.comments);
like = itemView.findViewById(R.id.like);
comment = itemView.findViewById(R.id.comment);
save = itemView.findViewById(R.id.save);
username = itemView.findViewById(R.id.username);
likes = itemView.findViewById(R.id.likes);
publisher = itemView.findViewById(R.id.publisher);
description = itemView.findViewById(R.id.description);
}
}
private void publisherInfo(final ImageView image_profile, final TextView username, final TextView publisher, final String userid){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Glide.with(mContext).load(user.getImageurl()).into(image_profile);
username.setText(user.getUsername());
publisher.setText(user.getUsername());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}

片段

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.HomeFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="180dp"
android:layout_height="35dp"
android:src="@drawable/zapster_logo"
android:id="@+id/logo"
android:layout_marginLeft="-20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/ic_inbox"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>

</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view_story"
android:layout_margin="5dp"
android:layout_below="@id/bar"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"
android:layout_below="@id/recycler_view_story"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<ProgressBar
android:id="@+id/progress_circular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>

Post模型

package com.example.zapster_picture_sharing.Model;
public class Post {
private String postid;
private String postimage;
private String description;
private String publisher;
public Post(String postid, String postimage, String description, String publisher){
this.postid = postid;
this.postimage = postimage;
this.description = description;
this.publisher = publisher;
}
public Post(){
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getPostimage() {
return postimage;
}
public void setPostimage(String postimage) {
this.postimage = postimage;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}

在XML中,实际上有两个androidx.recyclerview.widget.RecyclerView实例。

但是在您的代码中,您只将适配器附加到其中一个适配器上,从而导致此警告。

require add setLayoutManager

postAdapter = new PostAdapter(getContext(), postLists, true);
recyclerView.setAdapter(postAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView setLayoutManager (androidx.recyclerview.widget.RecyclerView.LayoutManager)

调用checkFollowing()方法后重新初始化postsList

postLists = new ArrayList<>();
postAdapter = new PostAdapter(getContext(), postLists, true);
recyclerView.setAdapter(postAdapter);
checkFollowing();

最新更新