Firebase实时数据库混乱

时间:2019-03-25 22:58:47

标签: android json firebase firebase-realtime-database

我正在尝试使用Firebase创建一个实时数据库,但是我的信息并未按原样存储。 JSON数据的格式如下:

enter image description here

如何摆脱djProfile的多个实例?字母和数字的随机字符串在该层次树中意味着什么?

这是我用来尝试检索此信息并将其存储在我的firebase recyclerview中的android类:

DjProfile类:

public class DjProfile
{
    String djName, uniqueID;


    public DjProfile(){}

    public DjProfile(String djName, String uniqueID)
    {
        this.djName = djName;
        this.uniqueID = uniqueID;
    }

    public String getDjName() { return djName; }
    public String getUniqueID() {return uniqueID; }
}

RecyclerView信息:

RecyclerView recyclerView = findViewById(R.id.dj_result_recycler);         recyclerView.setLayoutManager(new LinearLayoutManager(this));

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    Query query = rootRef.child("djProfile");

    FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
            .setQuery(query, DjProfile.class)
            .build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
    {

        @NonNull
        @Override
        public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);

            return new ResultsViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull ResultsViewHolder holder, int position, @NonNull DjProfile model) {
            holder.setDjProfile(model);
        }
    };
    recyclerView.setAdapter(firebaseRecyclerAdapter);
}

@Override
protected void onStart()
{
    super.onStart();
    firebaseRecyclerAdapter.startListening();
}

@Override
protected void onStop()
{
    super.onStop();
    if(firebaseRecyclerAdapter != null)
    {
        firebaseRecyclerAdapter.stopListening();
    }
}





public static class ResultsViewHolder extends RecyclerView.ViewHolder
{
    private TextView djNameView;

    public ResultsViewHolder(View itemView)
    {
        super(itemView);
        djNameView = itemView.findViewById(R.id.result_dj);
    }

    void setDjProfile(DjProfile profile)
    {
        String djName = profile.getDjName();
        djNameView.setText(djName);
    }
}

如何仅检索该djProfile中的名称?我想念什么?

任何反馈将不胜感激!

1 个答案:

答案 0 :(得分:1)

  

该随机字母和数字字符串在该层次树中意味着什么?

每次在引用上调用-Lar时,都会创建以push()开头的密钥。它相当于Firebase的数组索引。要了解有关它们的更多信息,请参见The 2^120 Ways to Ensure Unique IdentifiersBest Practices: Arrays in Firebase