读取android中firebase数据库中键的数组

时间:2017-11-01 13:14:42

标签: android firebase firebase-realtime-database firebaseui

我正在使用Firebase数据库,我将一些播放器详细信息保存为键的值列表。以下是我的表结构:

database structure screenshot

EventPlayer {}} {}} {} {{}}} eventID我将eventID作为EventPlayer推送玩家详情为eventID模型。

现在,在我的应用程序中,我想获取RecyclerView的播放器列表,并将其显示在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); 中。我尝试了各种组合,但我无法得到它。我的代码如下:

EventPlayer

修改

推送mref.child("EventPlayer").child(model.getEventID()).push().setValue(model); 的代码:

PageReference.StartPage

2 个答案:

答案 0 :(得分:2)

当您正在创建FirebaseRecyclerAdapter这样的对象时,您需要更改第4个参数:

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

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

答案 1 :(得分:1)

从当前的数据库结构中,您在EventPlayer节点下嵌套了2个唯一ID,因此FirebaseRecyclerAdapter可能会在列表中的错误级别检索数据。

您可以将推送代码调整为仅使用一个级别,方法是删除child(model.getEventID())

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

或者更改您的FirebaseRecyclerAdapter以改为使用直接引用:

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