所以我使用Firebase数据库来发布和检索一些数据。
发布数据工作正常,这是我的帖子树的样子:
树
这是我的发布方法(工作正常)
public void post_status(){
String full_song = song;
final String song_name = full_song.split("//")[0];
final String song_Artist = full_song.split("//")[1];
final DatabaseReference db_post = DB_NewPost.push();
if (song_name != null && song_Artist !=null && user_name != null && profile_image != null) {
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Posting...");
progressDialog.show();
DB_NewPost.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
db_post.child("artist").setValue(song_Artist);
db_post.child("song_name").setValue(song_name);
db_post.child("profile_image").setValue(profile_image);
db_post.child("user_name").setValue(user_name);
db_post.child("user_comment").setValue("Test Comment");
db_post.child("user_ID").setValue(user.getUid()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressDialog.dismiss();
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
CafeBar.builder(MainActivity.this)
.duration(CafeBar.Duration.MEDIUM)
.content("Post " + databaseError.getMessage())
.maxLines(4)
.theme(CafeBarTheme.Custom(Color.parseColor("#E65100")))
.show();
}
});
}
else {
CafeBar.builder(MainActivity.this)
.duration(CafeBar.Duration.MEDIUM)
.content("Post canceled, make sure all the values are set")
.maxLines(4)
.theme(CafeBarTheme.Custom(Color.parseColor("#E65100")))
.show();
}
}
这是我的RecylcerView相关代码:
@Override
public void onStart() {
super.onStart();
Query query = DB_NewPost.limitToLast(10);
FirebaseRecyclerOptions<user_post> options =
new FirebaseRecyclerOptions.Builder<user_post>()
.setQuery(query, user_post.class)
.build();
FBRA = new FirebaseRecyclerAdapter<user_post, postViewHolder>(options) {
@Override
public postViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_layout, parent, false);
return new postViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull postViewHolder holder, int position, @NonNull user_post model) {
holder.setUserName(model.getUsername());
holder.setComment(model.getUser_comment());
holder.setSongArtist("by: " + model.getSong_artist());
holder.setSongName(model.getSong_name());
holder.setPicture(getApplicationContext(), model.getImage());
Log.e("UserName", "" + model.getUsername());
Log.e("Comment", "" +model.getUser_comment());
Log.e("Artist", "" +model.getSong_artist());
Log.e("SongName", "" +model.getSong_name());
Log.e("Picture", "" +model.getImage());
}
};
mSongPost.setAdapter(FBRA);
FBRA.startListening();
}
public static class postViewHolder extends RecyclerView.ViewHolder{
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setSongName(String songName){
TextView song_name = mView.findViewById(R.id.song_name);
song_name.setText(songName);
}
public void setSongArtist(String Artist){
TextView song_artist = mView.findViewById(R.id.song_artist);
song_artist.setText(Artist);
}
public void setUserName(String userName){
TextView user_name = mView.findViewById(R.id.user_name);
user_name.setText(userName);
}
public void setComment(String comment){
TextView user_comment = mView.findViewById(R.id.user_comment_status);
user_comment.setText(comment);
}
public void setPicture(Context ctx,String image){
CircleImageView profile = mView.findViewById(R.id.photo_view);
RequestOptions options = new RequestOptions();
options.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter()
.circleCrop();
Glide.with(ctx)
.load(image)
.apply(options)
.into(profile);
}
}
这就是我面临问题的地方,我得到了 SongName 的字符串值和注释(即使它是固定的),但其他所有内容的值都是 NULL 值。我几乎尝试了所有内容,搜索了很多论坛和以前关于分离数据的问题等等。我找不到任何解决方案。有人愿意帮我吗?
这是我的日志:
01-30 16:29:52.736 25015-25015/xerxes.music_feed E/用户名:空 01-30 16:29:52.737 25015-25015/xerxes.music_feed E/注释:测试注释 01-30 16:29:52.737 25015-25015/xerxes.music_feed E/艺术家:空 01-30 16:29:52.737 25015-25015/xerxes.music_feed E/歌曲名称:一百 - 怪物 01-30 16:29:52.737 25015-25015/xerxes.music_feed E/图片:空
编辑:我也在我的日志中注意到了这一点,尽管我有这些变种的二传手和getter。
01-30 16:29:52.734 25015-25015/xerxes.music_feed W/ClassMapper:在类 xerxes.music_feed.user_post 上找不到profile_image的设置器/字段 01-30 16:29:52.734 25015-25015/xerxes.music_feed W/ClassMapper:在类 xerxes.music_feed.user_post 上找不到艺术家的二传手/字段 01-30 16:29:52.734 25015-25015/xerxes.music_feed W/ClassMapper:在类 xerxes.music_feed.user_post 上找不到user_ID的 setter/field 01-30 16:29:52.734 25015-25015/xerxes.music_feed W/ClassMapper:在类 xerxes.music_feed.user_post 上找不到user_name的二传手/字段
编辑 2:添加了二传手/二传手类
public class user_post {
private String username;
private String song_name;
private String song_artist;
private String user_comment;
private String image;
public user_post(){
}
public user_post(String username, String song_name, String song_artist, String user_comment,String p_image){
this.username = username;
this.song_name = song_name;
this.song_artist = song_artist;
this.user_comment = user_comment;
this.image = p_image;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSong_name() {
return song_name;
}
public void setSong_name(String song_name) {
this.song_name = song_name;
}
public String getSong_artist() {
return song_artist;
}
public void setSong_artist(String song_artist) {
this.song_artist = song_artist;
}
public String getUser_comment() {
return user_comment;
}
public void setUser_comment(String user_comment) {
this.user_comment = user_comment;
}
public String getImage() {
return image;
}
public void setUserImage(String image) {
this.image = image;
}
}
在你的POJO重构这一行中
private String username;
对此
private String user_name;
向可能遇到相同问题的人解释
当你用所有的二传手和getter做POJO类时,所有的Strings
或ints
,longs
等SHOULD
匹配与数据库结构相同的名称,并检查数据库中的数据是否有""
,例如phone_number:"359182"
,如果你的datbaase是这样的,是因为你需要在你的POJOString phone_number
中声明,并且NOT
长或phone_number...如果在数据库中phone_number: 3049512
phone_number
的值没有""
则意味着该值是int
、double
或long
快乐编码