我想根据帖子的时间戳对帖子进行排序。这些帖子包含标题和说明。我正在使用firebase来检索数据。但是视图中没有显示任何内容。视图持有者是空白的。我有什么问题吗?
public class AlertsActivity extends AppCompatActivity {
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mview;
public BlogViewHolder(View itemView) {
super(itemView);
mview = itemView;
}
public void setTitle(String title) {
TextView post_title = (TextView)mview.findViewById(R.id.blog_title);
post_title.setText(title);
}
public void setDesp(String desp) {
TextView post_desp = (TextView) mview.findViewById(R.id.blog_desp);
post_desp.setText(desp);
}
public void setTimestampCreated(HashMap<String, Object> timestamp) {
TextView show_ts = (TextView) mview.findViewById(blog_timestamp);
show_ts.setText(String.valueOf(timestamp));
}
}
适配器:
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mDatabase.keepSynced(true);
mDatabase.orderByChild("timestamp");
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.blog_row,
BlogViewHolder.class, mDatabase) {
@Override
protected void populateViewHolder(final BlogViewHolder viewHolder, final Blog model, int position) {
mDatabase.child("Blog").orderByChild("timestamp").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String alerts = dataSnapshot.getValue(String.class);
HashMap<String, Object> time = (HashMap<String, Object>) dataSnapshot.getValue();
viewHolder.setTitle(alerts);
viewHolder.setDesp(alerts);
viewHolder.setTimestampCreated(time);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mbloglist.setAdapter(firebaseRecyclerAdapter);
}
答案 0 :(得分:0)
对于这样一个简单的结构,您的代码看起来太复杂了。使用FirebaseUI适配器的主要目的是让您不必自己处理侦听器。据我所知,你只需要:
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
Query orderedBlogs = mDatabase.orderByChild("timestamp");
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.blog_row,
BlogViewHolder.class, orderedBlogs) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(blog.title);
viewHolder.setDesp(blog.desp);
viewHolder.setTimestampCreated(blog.timestamp);
}
};
mbloglist.setAdapter(firebaseRecyclerAdapter);
这是相应的最小Blog
类:
public class Blog {
public String desp;
public long timestamp;
public string title;
}