I have implemented the firebase recycler like this
public class HomeActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String REQUIRED = "Required";
// [START declare_database_ref]
private DatabaseReference mDatabase;
// [END declare_database_ref]
private FirebaseAuth mAuth;
private static String LOG_TAG = "CardViewActivity";
public static class PostHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View mView;
public PostHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mView = itemView;
}
public void setName(String name) {
TextView field = (TextView) mView.findViewById(R.id.name);
field.setText(name);
}
public void setText(String text) {
TextView field = (TextView) mView.findViewById(R.id.body);
field.setText(text);
}
public void setImage(String image){
ImageView pp = (ImageView) mView.findViewById(R.id.image);
Picasso.with(Application.getAppContext()).load(image).placeholder(R.drawable.nodp).error(R.drawable.nodp).transform(new CircleTransform()).into(pp);
}
@Override
public void onClick(View mView) {
//what to do here
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
findViewById(R.id.progressBar3).setVisibility(View.VISIBLE);
findViewById(R.id.nopost).setVisibility(View.GONE);
//mListview = (ListView) findViewById(R.id.listview);
mAuth = FirebaseAuth.getInstance();
//the post list shit is happening here
final DatabaseReference root = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = mAuth.getCurrentUser();
DatabaseReference postRef = root.child("users").child(user.getUid().toString()).child("posts");
RecyclerView recycler = (RecyclerView) findViewById(R.id.recyclerview);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<PostList, PostHolder>(PostList.class, R.layout.row_one, PostHolder.class, postRef) {
@Override
public void populateViewHolder(PostHolder postViewHolder, PostList postList, int position) {
//try catch block to catch events of no posts, it will most likely return a null error, so im catching it, else
//find its exception and catch it
try {
String firstname = postList.getFirstname();
String lastname = postList.getLastname();
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1); //convert first string to uppercase
lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);// same thing happening here
String name = (firstname + " " + lastname); // concatenate firstname and lastname variable.
postViewHolder.setName(name);
postViewHolder.setText(postList.getBody()); // set the vew holder
//note that picasso view holder was applied in the view holder instead
String image = postList.getImgUrl().toString();
postViewHolder.setImage(image);
findViewById(R.id.progressBar3).setVisibility(View.GONE);
}
catch(NullPointerException e) {
findViewById(R.id.nopost).setVisibility(View.VISIBLE);
}
}
};
recycler.setAdapter(mAdapter);
}
}
and it works perfectly for my use case, I have gone through questions about how to set up onClick listeners for recycler adapter, and I think I've got it as seen in my code above.
My question is:
- minor issue: when an item is clicked, I want to pass data through an intent to another activity to display details about the post. I can achieve this through
intent.putExtra()
on my ViewHolder as referenced by another question. But my problem is that my view holder class is outside the activity'sonCreate()
(that's the only way it seemed to work) so I can't call start activity from within ViewHolder, major issue: since
PostList
, from which my data is from, is not yet set in the ViewHolder, so I can't putpostList.getName()
andgetBody()
in an intent, but I think it would be okay if myonClick()
can go inside the firebase block itself, here:FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<PostList, PostHolder>(PostList.class, R.layout.row_one, PostHolder.class, postRef) { @Override public void populateViewHolder(PostHolder postViewHolder, PostList postList, int position) { //try catch block to catch events of no posts, it will most likely return a null error, so im catching it, else //find its exception and catch it try { String firstname = postList.getFirstname(); String lastname = postList.getLastname(); firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1); //convert first string to uppercase lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);// same thing happening here String name = (firstname + " " + lastname); // concatenate firstname and lastname variable. postViewHolder.setName(name); postViewHolder.setText(postList.getBody()); // set the vew holder //note that picasso view holder was applied in the view holder instead String image = postList.getImgUrl().toString(); postViewHolder.setImage(image); findViewById(R.id.progressBar3).setVisibility(View.GONE); } catch(NullPointerException e) { findViewById(R.id.nopost).setVisibility(View.VISIBLE); } } //onclick goes here with all the put extra and startactivity stuff }; recycler.setAdapter(mAdapter);