这是我在firebase
中从RecyclerView
数据库获取帖子的代码,
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Post,PostViewFolder> PSVF=new FirebaseRecyclerAdapter<Post, PostViewFolder>(
Post.class,
R.layout.singlepostitem,
PostViewFolder.class,
mDatabse) {
@Override
protected void populateViewHolder(PostViewFolder viewHolder, Post model, int position) {
viewHolder.setTitle(model.getPosttitle());
viewHolder.setDesc(model.getPostdesc());
viewHolder.setImage(getApplicationContext(),model.getPostimage());
final String post_key=getRef(position).getKey().toString();
viewHolder.mview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(PostList.this,Singlepost.class);
intent.putExtra("post_id",post_key);
startActivity(intent);
}
});
}
};
mpostlist.setAdapter(PSVF);
}
public static class PostViewFolder extends RecyclerView.ViewHolder{
View mview;
public PostViewFolder(View itemView) {
super(itemView);
mview=itemView;
}
public void setTitle(String posttitle){
TextView post_title=(TextView) mview.findViewById(R.id.posttitle);
post_title.setText(posttitle);
}
public void setDesc(String postdesc){
TextView post_desc=(TextView) mview.findViewById(R.id.postdescription);
post_desc.setText(postdesc);
}
public void setImage(Context ctx, String postimage){
ImageView post_image=(ImageView) mview.findViewById(R.id.postimage);
Picasso.with(ctx).load(postimage).into(post_image);
}
}