gpt4 book ai didi

android - FirebaseRecyclerAdapter 无法从 Firebase 数据库中检索数据

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:00 38 4
gpt4 key购买 nike

我正在尝试在我的 MainActivity(主页)上显示博客文章。但是主页没有显示我的意思是主页是空的。

Firebase 数据库结构:

enter image description here

主要 Activity :

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView mBlogList;
private DatabaseReference mDataRef;
private FirebaseRecyclerAdapter<BlogInfo, ViewHolder> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.d(TAG, "onCreate: started!");
mDataRef = FirebaseDatabase.getInstance().getReference().child("BlogApp").child("Posts");
mBlogList = findViewById(R.id.blogListId);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));

FirebaseRecyclerOptions<BlogInfo> options = new FirebaseRecyclerOptions.Builder<BlogInfo>()
.setQuery(mDataRef, BlogInfo.class).build();

adapter = new FirebaseRecyclerAdapter<BlogInfo, ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull BlogInfo model) {

String imgUrl = model.getImage();
Picasso.get().load(imgUrl).into(holder.post_image);
holder.post_title.setText(model.getTitle());
holder.post_desc.setText(model.getDescription());

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: initialized!");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_blog_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
};
mBlogList.setAdapter(adapter);

}

@Override
protected void onStart() {
super.onStart();
adapter.startListening();
Log.d(TAG, "onStart: init");
}

@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
Log.d(TAG, "onStop: stop Blog list");
}

// ViewHolder Class
public static class ViewHolder extends RecyclerView.ViewHolder{
private static final String TAG = "ViewHolder";
TextView post_title;
TextView post_desc;
ImageView post_image;

ViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "ViewHolder: Initialized!");
post_title = itemView.findViewById(R.id.postTitleId);
post_desc = itemView.findViewById(R.id.postDescId);
post_image = itemView.findViewById(R.id.postImageId);
}
}
}

BlogInfo.class:

public class BlogInfo {
private String description;
private String image;
private String title;

public BlogInfo() {}

public BlogInfo(String title, String description, String image) {
this.title = title;
this.description = description;
this.image = image;
}

public String getTitle() {return title;}

public void setTitle(String title) {this.title = title;}

public String getDescription() {return description;}

public void setDescription(String description) {this.description = description;}

public String getImage() {return image;}

public void setImage(String image) {this.image = image;}
}

这是我第一次尝试使用 FirebaseRecyclerAdapter 从 Firebase 数据库中检索数据。但我不知道问题出在哪里。我也尝试调试,但编译器不会进入 onCreateViewHolderonBindViewHolder 函数。但我完全关注 FirebaseUI for Realtime Database引用。

最佳答案

要解决这个问题,您需要删除以下代码行:

mBlogList.setHasFixedSize(true);

仅当您希望 RecyclerView 具有固定大小时才需要这样做。在您的情况下没有必要。

关于android - FirebaseRecyclerAdapter 无法从 Firebase 数据库中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625727/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com