1

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:

  1. 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's onCreate() (that's the only way it seemed to work) so I can't call start activity from within ViewHolder,
  2. major issue: since PostList, from which my data is from, is not yet set in the ViewHolder, so I can't put postList.getName() and getBody() in an intent, but I think it would be okay if my onClick() 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);
    
Sufian
  • 6,405
  • 16
  • 66
  • 120
Mordred
  • 185
  • 2
  • 15
  • 1
    Why is the list null? That data should come from Firebase. And you don't need to catch the NullPointerException, simply check if the list variable is null, else do what you want – OneCricketeer Aug 24 '16 at 13:37
  • Possible duplicate of [how to implement a SetOnItemClickListener FirebaseRecyclerViewAdapter](http://stackoverflow.com/questions/34110497/how-to-implement-a-setonitemclicklistener-firebaserecyclerviewadapter) – OneCricketeer Aug 24 '16 at 13:42
  • @cricket_007 yes, if the inner class would be non static – aschattney Aug 24 '16 at 13:43
  • Ignore the null list try catch thing, It doesnt really affect my code at all.. just something im experimenting on – Mordred Aug 24 '16 at 14:04

2 Answers2

0

You mean that you cannot get the origin activity to create the intent? have you tried getContext(), getActivity() or getBaseContext()?

Context context = getContext();
final Intent intent =  new Intent(context, FirstActivity.class);
// all your stuff
context.startActivity(intent);
adalpari
  • 3,052
  • 20
  • 39
  • I guess this would work for the minor issue, thank you. How about the major issue, its is really important for the overall thing to work together – Mordred Aug 24 '16 at 13:44
  • if it's not necessary to recognize which view was pressed, then try this: http://stackoverflow.com/questions/24471109/recyclerview-onclick/26196831#26196831 – aschattney Aug 24 '16 at 13:57
  • it is necessary as i am grabbing data from that view to pass to a function in the next activity – Mordred Aug 24 '16 at 14:01
  • i meant a specific view in one element – aschattney Aug 24 '16 at 14:12
0

If you want to perform good item click functionality with a proper way then create a separate class for FirebaseRecyclerAdapter and use Interface as item click listener so you can perform intent functionality from HomeActivity.

OR

Create reference of HomeActivity class and use that reference at

@Override
public void onClick(View mView) {
    //what to do here
    //mcontect will be your class reference 
    Intent i = new (mcontext,SecondActivity.class);
    startActivity(i);
}
Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
ViramP
  • 1,659
  • 11
  • 11