如何在Fire base android中获取“特殊子节点”?

时间:2018-07-11 07:02:00

标签: android firebase firebase-realtime-database firebaseui

Firebase Db Structure

我需要在Recyler视图中从AGS Biryani获取畅销书值,不知道如何基于父节点将子值添加到Recyler适配器中

模型

public class Food_List {
    private String itemname;
    private String itemdescrp;
    private long itemnewprice;
    private  long itemoldprice;


    public String getItemname() {
        return itemname;
    }

    public void setItemname(String itemname) {
        this.itemname = itemname;
    }

    public String getItemdescrp() {
        return itemdescrp;
    }

    public void setItemdescrp(String itemdescrp) {
        this.itemdescrp = itemdescrp;
    }

    public long getItemnewprice() {
        return itemnewprice;
    }

    public void setItemnewprice(long itemnewprice) {
        this.itemnewprice = itemnewprice;
    }

    public long getItemoldprice() {
        return itemoldprice;
    }

    public void setItemoldprice(long itemoldprice) {
        this.itemoldprice = itemoldprice;
    }

}

Food_List_ViewHolders

ViewHolders将值添加到Recyler视图中

public class Food_List_ViewHolders extends RecyclerView.ViewHolder {

    View mView;
    private Context context;
    Context mContext;
    String nodata;
    public Food_List_ViewHolders(View itemView) {

        super(itemView);
        mView=itemView;
    }

    @SuppressLint("SetTextI18n")
    public  void setDetails(Context applicationContext, final String itemname, String itemdescrp,

                            final long itemnewprice,
                            final long itemoldprice
                            )

    {

        final TextView dishitemname=mView.findViewById(R.id.dishheader);
        TextView dishitemnamedescrp=mView.findViewById(R.id.dishheaderdescrp);
        TextView dishitemnameoldprice=mView.findViewById(R.id.itemoldprice);
        TextView dishitemnamenewprice=mView.findViewById(R.id.itemnewprice);
        dishitemname.setText(itemname);
        dishitemnamedescrp.setText(itemdescrp);
        dishitemnamenewprice.setText(applicationContext.getString(R.string.Rup) +  itemnewprice);
        dishitemnameoldprice.setText(applicationContext.getString(R.string.Rup) +  itemoldprice);


        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View mView) {

                Context context = mView.getContext();
            }

        });

    }


}

主要活动

不知道如何在DataSnapshot中添加子级值

mRecycleriew =findViewById(R.id.my_recycler_views);
        mRecycleriew.setLayoutManager(new LinearLayoutManager(this));
        mFirebaseDatabase= FirebaseDatabase.getInstance();
        mRef=mFirebaseDatabase.getReference().child("restaurants").equalTo("AGS Biryani");
        //DatabaseReference restaurantsRef =  mRef.child("restaurants");
        mRef(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()){


                progressDoalog.dismiss();

            }

        }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });




    }

    private void mRef(ValueEventListener valueEventListener) {
    }


    protected void onStart() {

        super.onStart();
        FirebaseRecyclerAdapter<Food_List,Food_List_ViewHolders> firebaseRecyclerAdapter=
                new FirebaseRecyclerAdapter<Food_List, Food_List_ViewHolders>(
                        Food_List.class,
                        R.layout.item_child,
                        Food_List_ViewHolders.class,
                        mRef)
                {
                    @Override
                    protected void populateViewHolder(Food_List_ViewHolders viewHolder, Food_List model, int position) {
                        viewHolder.setDetails(getApplicationContext(),model.getItemname(),model.getItemdescrp(),model.getItemnewprice(),model.getItemoldprice());

                    }


                };

        mRecycleriew.setAdapter(firebaseRecyclerAdapter);

    }

1 个答案:

答案 0 :(得分:1)

本质上,您正在做

mRef=mFirebaseDatabase.getReference().child("restaurants").equalTo("AGS Biryani");
FirebaseRecyclerAdapter<Food_List,Food_List_ViewHolders> firebaseRecyclerAdapter=
            new FirebaseRecyclerAdapter<Food_List, Food_List_ViewHolders>(
                    Food_List.class,
                    R.layout.item_child,
                    Food_List_ViewHolders.class,
                    mRef)

这表示您要显示所有优先级为AGS Biryani的餐厅。那不是您要尝试的操作,因此您需要修改自己的参考:

mRef=mFirebaseDatabase.getReference().child("restaurants/AGS Biryani/bestsellers");

当您将此参考文献传递到FirebaseRecyclerAdapter时,它将显示AGS Biryani的所有畅销书。