gpt4 book ai didi

java - 我无法使用 FirebaseRecyclerAdapter 从 Firebase 数据库检索数据

转载 作者:行者123 更新时间:2023-12-01 16:37:29 26 4
gpt4 key购买 nike

将我的数据添加到 Firebase 实时数据库后,它已成功更新。然后我想获取我添加的所有数据以在我的主要 Activity 中显示。编写完下面附加的代码后,我没有看到任何语法错误,但如果我运行该应用程序,我似乎无法在主要 Activity 中查看数据,而且我似乎无法找出错误。如果有人能帮助我,我将不胜感激。谢谢大家。

这是我的模型。


package com.example.myapplication.Activities;

public class Posts {

public String date, description,fullName,postImage,profileImage,time,uid;

public Posts(){

}

public Posts(String date, String description, String fullName, String postImage, String profileImage, String time, String uid) {
this.date = date;
this.description = description;
this.fullName = fullName;
this.postImage = postImage;
this.profileImage = profileImage;
this.time = time;
this.uid = uid;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getDescription() {
return description;
}

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

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public String getPostImage() {
return postImage;
}

public void setPostImage(String postImage) {
this.postImage = postImage;
}

public String getProfileImage() {
return profileImage;
}

public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}
}

这是我的查询


private DatabaseReference databaseReference, postRef;
databaseReference = FirebaseDatabase.getInstance().getReference().child("users");
postRef = FirebaseDatabase.getInstance().getReference().child("posts");


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.Maintoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);

LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);



displayAllUserPosts();
}

使用 FirebaseRecyclerAdapter


private void displayAllUserPosts() {

FirebaseRecyclerOptions<Posts> options =
new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(postRef, Posts.class)
.build();



firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull PostViewHolder postViewHolder, int i, @NonNull Posts posts) {

postViewHolder.setDate(posts.getDate());
postViewHolder.setDescription(posts.getDescription());
postViewHolder.setFullName(posts.getFullName());
postViewHolder.setTime(posts.getTime());
postViewHolder.setPostImage(getApplicationContext(),posts.getPostImage());
postViewHolder.setProfileImage(getApplicationContext(), posts.getProfileImage());
}

@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.all_post_layout, parent, false);

return new PostViewHolder(view);

}
};

recyclerView.setAdapter(firebaseRecyclerAdapter);

}


public static class PostViewHolder extends RecyclerView.ViewHolder{
View view;

public PostViewHolder(@NonNull View itemView) {
super(itemView);
view = itemView;
}

public void setFullName(String fullName){
TextView username = (TextView) view.findViewById(R.id.post_user_name);
username.setText(fullName);
}

public void setProfileImage(Context context, String profileImage){
CircleImageView profileimage = (CircleImageView) view.findViewById(R.id.post_profile_image);
Picasso.with(context).load(profileImage).into(profileimage);
}

public void setDate( String date) {
TextView Date = (TextView)view.findViewById(R.id.post_date);
Date.setText(" " + date);
}

public void setTime(String time) {
TextView Time = (TextView)view.findViewById(R.id.post_time);
Time.setText(" " + time);
}

public void setDescription(String description) {
TextView Description = (TextView)view.findViewById(R.id.post_description);
Description.setText(description);
}

public void setPostImage( Context context, String postImage){
ImageView postimage = (ImageView)view.findViewById(R.id.post_image);
Picasso.with(context).load(postImage).into(postimage);
}

}

这是我的数据库树

posts
gjZxz9HL4IblUAcdWz788Z9AdXu118
May
202023:44
date:
"18/May/2020"
description:
"my first"
fullName:
"emeka"
postImage:
"https://firebasestorage.googleapis.com/v0/b/soc..."
profileImage:
"https://firebasestorage.googleapis.com/v0/b/soc..."
time:
"23:44"
uid:
"gjZxz9HL4IblUAcdWz788Z9AdXu1"
gjZxz9HL4IblUAcdWz788Z9AdXu120
May
2020:00:55
date:
"20/May/2020"
description:
"I like this"
fullName:
"emeka"
postImage:
"https://firebasestorage.googleapis.com/v0/b/soc..."
profileImage:
"https://firebasestorage.googleapis.com/v0/b/soc..."
time:
":00:55"
uid:
"gjZxz9HL4IblUAcdWz788Z9AdXu1"

如果有人可以的话,请帮助我。我会很感激。谢谢。

这是我运行应用程序时得到的结果,如下图 enter image description here

最佳答案

我找到了答案,我正在使用 addOnCompleteListener 来获取我的帖子图像的下载 URL,而不是 addOnSuccesslistener

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

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