如何在随机密钥中检索我的数据?

时间:2018-09-15 12:45:46

标签: android firebase-realtime-database

我想在随机密钥中获取数据,如下所示:

description

我正在使用Firebase,并且已经尝试了很多方法,但是我无法解决此问题。

public class MainActivity extends AppCompatActivity {

    public RecyclerView recyclerView , recyclerView1 ;
    public DatabaseReference ref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        {
            ref = FirebaseDatabase.getInstance().getReference().child("Requests");
            recyclerView = (RecyclerView) findViewById(R.id.recycler_food);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));

            FirebaseRecyclerAdapter<Food, FoodViewHolder> adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(
                    Food.class,
                    R.layout.individual_row,
                    FoodViewHolder.class,
                    ref
            ) {
                @Override
                protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {

                    viewHolder.setProductName(model.getProductName());
                    viewHolder.setQuantity(model.getQuantity());

                }
            };
            LinearLayoutManager layoutManager
                    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
            recyclerView.setAdapter(adapter);
        }

        ref = FirebaseDatabase.getInstance().getReference().child("Requests");
        recyclerView1 = (RecyclerView) findViewById(R.id.recycler_foods);
        recyclerView1.setHasFixedSize(true);
        recyclerView1.setLayoutManager(new LinearLayoutManager(this));

        FirebaseRecyclerAdapter<Food, FoodViewHolder> adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(
                Food.class,
                R.layout.individual_row,
                FoodViewHolder.class,
                ref
        ) {
            @Override
            protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {

                viewHolder.setProductName(model.getProductName());
                viewHolder.setQuantity(model.getQuantity());

            }
        };
        LinearLayoutManager layoutManager
                = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView1.setAdapter(adapter);
    }

    public static class FoodViewHolder extends RecyclerView.ViewHolder {

        TextView text_productName , text_quantity , text_customername;
        public FoodViewHolder(View itemView) {
            super(itemView);
            text_productName = (TextView) itemView.findViewById(R.id.food_name);
            text_quantity = (TextView) itemView.findViewById(R.id.food_qty);


        }

        public void setProductName(String productName) {
            text_productName.setText(productName);

        }

        public void setQuantity(String quantity) {
                text_quantity.setText(quantity);

        }
    }

1 个答案:

答案 0 :(得分:0)

您正在Requests上创建适配器。这意味着回收者视图将显示一个请求列表:因此,每个请求一行。但是您创建了一个数据类Food和一个FoodViewHolder,因此看来您实际上是想显示食物列表

要在“回收者”视图中显示食物列表,数据库中必须具有食物列表。例如,您可以通过确定该请求的DatabaseReference并在适配器中显示它来显示第一个请求的食物:

DatabaseReference requestsRef = FirebaseDatabase.getInstance().getReference().child("Requests");
Query firstRequestQuery = requestsRef.orderByKey().limitToFirst(1);
firstRequestQuery.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot requestSnapshot: dataSnapshot.getChildren()) {
        FirebaseRecyclerAdapter<Food, FoodViewHolder> adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(
                Food.class,
                R.layout.individual_row,
                FoodViewHolder.class,
                requestSnapshot.getRef().child("foods")
        ) {
            @Override
            protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
                viewHolder.setProductName(model.getProductName());
                viewHolder.setQuantity(model.getQuantity());
            }
        };
        recyclerView.setAdapter(adapter);
    }
  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    throw databaseError.toException();
  }
}

虽然代码可能存在一些问题,但它演示了如何在“回收者”视图中显示第一个请求的内容。密钥为requestSnapshot.getRef().child("foods"),可确保适配器加载第一个请求的食物。