How to use a data from one table as a reference to get another data in another table using Firebase?

Kim E. R

I just retrieve a list of data from a table named 'appointment'. From this table, I will need to get a data named 'theraid' in order to get the name from another table named 'thera'. I have no idea how to do it in android java. Is there someone can guide me? Thanks in advance.

Here is my table structure in firebase table

Here is my code, but it does not work for me. Java class

 databaseReference= FirebaseDatabase.getInstance().getReference().child("appointment");

        databaseReference.orderByChild("userid").equalTo(userid1).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


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

                    AppointmentObject thera= dataSnapshot1.getValue(AppointmentObject.class);
                    String tid = dataSnapshot.child("theraid").getValue(String.class);

                    a.add(thera);

                     refThera = FirebaseDatabase.getInstance().getReference("alluser").child("thera");
                    refThera.child(tid).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            for(DataSnapshot dataSnapshot2: dataSnapshot.getChildren()) {
                                String text = dataSnapshot2.child("name").getValue(String.class);
                                tname.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,tname);
        rv.setAdapter(adapter);

RecyclerView class


    @Override
    public void onBindViewHolder(@NonNull final MyRecyclerviewPAppointment.MyViewHolder holder, int position) {
        holder.tname.setText(name.get(position).charAt(position));
        holder.tdate.setText(alist.get(position).getDate());
        holder.ttime.setText(alist.get(position).getTiming());

    }
Peter Haddad

Try the following:

 databaseReference = FirebaseDatabase.getInstance().getReference().child("appointment");

        databaseReference.orderByChild("userid").equalTo(userid1).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
                    String theraid = dataSnapshot1.child("theraid").getValue(String.class);

                    DatabaseReference refThera = FirebaseDatabase.getInstance().getReference().child("alluser").child("thera");
                    refThera.child(theraid).addValueEventListener(new ValueEventListener() {
                    @Override
                   public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                       String name = dataSnapshot.child("name").getValue(String.class);
                       }

                    @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();
                throw databaseError.toException();
            }
        });

First retreive the theraid inside the appointment node then add another databaseReference and use the theraid to be able to retrieve the name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to copy data from one table to another table in Firebase?

How to get One Table data on the basis of ID from another table

How to get data from another table according to a retrieved data in Firebase

How do I get a data from one table to another but with calculations?

Get data from one table by reference of another table's column's count(*)

Update records of one table using data from another table

Append data from one table to another table

Copy Data from one table to another table

How to copy data from one table to another and then delete that data in first table using Java?

SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

How to copy data from one table to another new table in MySQL?

How do i copy data from one table to another table?

How to copy data from one table to another table based on criteria

How to move a field from one table to another table and save the data

How to Replace and Update Data From One Table to Another Table in MySQL

How to transfer data from one table of a database to a table in another database

Laravel querying to get data from a table using another table and an input

In Pandas how can I use the values in one table as an index to extract data from another table?

How to copy data from a table and use it to update another table

how can I get data from a table that is liked to a table that is liked to another one it self?

How to get data from a table and move to another table in jquery

Pulling data from one table to use in another query for SQL Server?

How to import data from one table to another table using Database: Seeding? (laravel 5.3)?

how to insert data from one table to another table using type in plsql

In a SELECT command, how do I use data from one table to specify data in another?

How to update oracle table in Python using data from another table

How to copy data from one table to another using one by one row

How to get an avarage of something from a table using data from another table

Get a column data from one table and update it to another column from another table - MYSQL