0

I am working with a Firebase Database where I am saving some player details as a list in value for a key. Following is my table structure:

database structure screenshot

Here in EventPlayer I have eventID as key and for eventID I am pushing Player details as EventPlayer model.

Now in my application I want to fetch the list of players for an eventID and display it in RecyclerView. I have tried various combinations but I am not able to get it. My code below:

FirebaseRecyclerAdapter<UserDataClass, EventPlayers.EventPlayerViewHolder> adapter= new FirebaseRecyclerAdapter<UserDataClass, EventPlayers.EventPlayerViewHolder>(
            UserDataClass.class,
            R.layout.eventplayercard,
            EventPlayers.EventPlayerViewHolder.class,
            mref.child("EventPlayer").orderByChild("eventID").equalTo(eventId)
    ) {
        @Override
        protected void populateViewHolder(EventPlayers.EventPlayerViewHolder viewHolder, final UserDataClass model, int position) {
            viewHolder.PlayerName.setText(model.getName());
            viewHolder.PlayerPosition.setText("Position: "+model.getPosition());
            viewHolder.PlayerFoot.setText("Foot: "+model.getPreffoot());
            viewHolder.PlayerKnownAs.setText("Event Date: "+model.getKnownas());
            final String key = this.getRef(position).getKey();

            viewHolder.btnRemove.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    AlertDialog.Builder alert = new AlertDialog.Builder(EventPlayers.this);
                    alert.setTitle("Remove Player");
                    alert.setMessage("Are you sure you want to remove player?");

                    alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            mref.child("EventRequest").child(mAuth.getCurrentUser().getUid()).child(key).setValue(null);
                        }
                    });
                    alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });

                    alert.show();
                }
            });
        }
    };

    recyclerView.setAdapter(adapter);

Edit

Code to push EventPlayer:

mref.child("EventPlayer").child(model.getEventID()).push().setValue(model);
6
  • 1
    Can you share your code where you are pushing the EventPlayer model onto the database? It looks like you have nested 2 pushes here. Commented Nov 1, 2017 at 13:17
  • there are two levels key values under EventPlayer node. make it one and it'll work Commented Nov 1, 2017 at 13:17
  • @Grimthorr I have updated my code please check Edit. Commented Nov 1, 2017 at 13:19
  • @RahulKumar I am pushing values for EventID key, The keys are generated when I push any value in firebase. Is there a way to reduce it? Commented Nov 1, 2017 at 13:21
  • yes.. better way is to use the pojo to push the value so that these nesting won't be happening. Commented Nov 1, 2017 at 13:22

2 Answers 2

2

You need to change the 4th argument when your are creating an object of FirebaseRecyclerAdapter like this:

mref.child("EventPlayer").orderByChild("eventID").equalTo(eventId)

with

mref.child("EventPlayer").child(model.getEventID()).orderByChild("eventID").equalTo(eventId)
Sign up to request clarification or add additional context in comments.

6 Comments

Change model.getEventID() with an actual event id. Does it work?
And? What happened?
All the properties of my model in populateViewHolder are null
I have used this: mref.child("EventPlayer").child(eventId).orderByChild("eventID").equalTo(eventId)
I think I found the mistake in my code, the name of properties are not matching in my app model and firebase model. I am correcting it and will check it then.
|
1

From your current database structure, you are nesting 2 unique IDs under the EventPlayer node, so the FirebaseRecyclerAdapter is likely retrieving data at the wrong level in the list.

You could adjust your push code to only use one level, by removing child(model.getEventID()):

mref.child("EventPlayer").push().setValue(model);

Or change your FirebaseRecyclerAdapter to use the direct reference instead:

mref.child("EventPlayer").child(eventId);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.