1

I have used FirebaseRecyclerView from Firebase UI to show a list of data. Now when I click on one of the items, depending on the ID of the item in database, I want to open DetailActivity. How can I get Firebase generated ID(eg: -KRAVjAhr6A_spgJZEET) in populateViewHolder.

Query queryRef = mDatabase.child("user-tickets").child(userId);
FirebaseRecyclerAdapter<Ticket, TicketViewHolder> adapter = new FirebaseRecyclerAdapter<Ticket, TicketViewHolder>(
            Ticket.class,
            R.layout.item_ticket,
            TicketViewHolder.class,
            queryRef
    ) {
        @Override
        protected void populateViewHolder(TicketViewHolder viewHolder, Ticket model, int position) {
            //want to get Id here for this record
        }
    };
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Saad Usmani
  • 173
  • 14

2 Answers2

1

you must put your data in a ArrayList then on your populateView attche onClickListener on your view and pass the id that is clicked using a Bundle, remember you have set your view to clickable="true" on your xml if its not a button.

viewHolder.container.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bundle bundle = new Bundle();
            bundle.putString("ID_KEY", items.get(position).getId());

            Intent intent = new Intent(context.getApplicationContext(), DetailActivity.class);
            intent.putExtra("EXTRAS_KEY", bundle);
            context.startActivity(intent);
        }
    });
pastillas
  • 81
  • 6
  • I dont have any ArrayList for data, as FirebaseRecyclerView Handles it internally. It only gives control in populateViewHolder, which gets called for each item in the firebase node. – Saad Usmani Sep 09 '16 at 13:34
  • you can pass the id, it is accessible on your model(Ticket model) class unless the id on your model is declared private, in that case you have to add getter function to get the value and put in Bundle. – pastillas Sep 10 '16 at 00:02
1

I have implemented one TouchListener for each Item click, in the onClick callback, depending on the position, you can get the key as shown:

mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, mRecyclerView, new RecyclerClickListener() {
        @Override
        public void onClick(View view, int position) {
            String key = adapter.getRef(position).getKey();
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

Credit: How to get obj key from FirebaseListAdapter on Item Click. FirebaseUI

Community
  • 1
  • 1
Saad Usmani
  • 173
  • 14