【问题标题】:How to get a specific data and use it as a key to get another data from another table in firebase如何获取特定数据并将其用作从 firebase 中另一个表中获取另一个数据的键
【发布时间】:2019-08-06 05:44:46
【问题描述】:

我需要从一个表中获取数据并将其用作参考,以便在另一个表中获取另一个数据。

这是我的表结构

这是我在 java 类中获取数据的方法。在我的表中,我存储了一个名为“theraid”的属性,我需要检索它并将其用作参考,以便在其中获取另一个名为“name”的属性另一个表。

 a=new ArrayList<AppointmentObject>();
 namelist=new ArrayList<String>();

        databaseReference= FirebaseDatabase.getInstance().getReference().child("appointment");
        databaseReference.orderByChild("userid").equalTo(userid1).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {


                for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {

                    AppointmentObject thera= dataSnapshot1.getValue(AppointmentObject.class);
                    a.add(thera);
                    final String theraid = dataSnapshot1.child("theraid").getValue().toString();


                    refThera = FirebaseDatabase.getInstance().getReference().child("alluser").child("thera");
                    refThera.child(theraid).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot3) {

                            for (DataSnapshot ds: dataSnapshot3.getChildren())
                            {
***************error this line below********************************
                                String text = ds.child("name").getValue().toString();
                                namelist.add(text);
                            }
                        }
                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
                            throw databaseError.toException();
                        }
                    });

                }


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
            }
        });
adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a,namelist);
                rv.setAdapter(adapter);

这里是 recyclerview 类,用于显示通过 java 类检索的数据。

public class MyRecyclerviewPAppointment extends RecyclerView.Adapter<MyRecyclerviewPAppointment.MyViewHolder> {
    private Context context;
    ArrayList<AppointmentObject> alist;
ArrayList<String> namelist1;

    public MyRecyclerviewPAppointment(Context c, ArrayList<AppointmentObject> t,ArrayList<String> namelist) {
        context = c;
        alist = t;
        namelist1=namelist;
    }
@Override
    public void onBindViewHolder(@NonNull final MyRecyclerviewPAppointment.MyViewHolder holder,final int position) {

     holder.tdate.setText(alist.get(position).getDate());
      holder.ttime.setText(alist.get(position).getTiming());
      holder.tname.setText(namelist1.get(position));

        // holder.tstatus.setText(alist.get(position).getTiming());
    }

    @Override
    public int getItemCount() {
        return alist.size();
    }
}

我不知道我的代码出了什么问题,但它无法工作,它只是一直停止。 这是错误。我可以知道我的 d=code 中的错误在哪里吗?谢谢你帮助我:)。

【问题讨论】:

    标签: java android firebase firebase-realtime-database foreign-keys


    【解决方案1】:

    在第二个侦听器中,您的databaseReference 位于节点theraid。因此,您不必循环访问name 的值。所以修改如下代码:

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot3) {
        for (DataSnapshot ds: dataSnapshot3.getChildren()) {
            String text = ds.child("name").getValue().toString();
            namelist.add(text);
        }
    }
    

    进入这个:

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot3) {
        String text = dataSnapshot3.child("name").getValue().toString();
        namelist.add(text);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-20
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多