简体   繁体   中英

How to get a specific data and use it as a key to get another data from another table in firebase

I need to get data from a table and use it as a reference in order to get another data in another table.

Here is my table structure 表

Here is how I get the data in java class.IN my table, I have stored an attribute called 'theraid' which I need to retrieve it and use it as a reference in order to get another attribute named 'name' in another table.

 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);

Here is the recyclerview class in order to display the data that retrieve via the java class.

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();
    }
}

I don't know what's wrong to my code but it can't work, it just keeps stopped. Here is the error. May I know where is the mistake in my d=code? Thanks for helping me :). 错误

In the second listener, your databaseReference is at node theraid . Therefore you don't have to loop to be able to access the value of name . So change the following code:

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

Into this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM