将我的数据添加到Firebase实时数据库后,它已成功更新。然后我想获取我添加的所有数据以显示在我的主要活动中。编写了我在下面附加的代码后,我没有看到任何语法错误,但是如果我运行该应用程序,我似乎无法在我的主要活动中查看我的数据,并且我似乎无法找出错误。如果有人能帮助我,我将不胜感激。谢谢大家。
这是我的模型。
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"
如果有人可以的话,请帮助我。我将不胜感激。谢谢。
这是我运行应用程序时得到的,下图 在此处输入图像描述
我找到了答案,我正在使用addOnCompleteListener来获取我的帖子图片的下载URL,而不是addOnSuccesslistener