简体   繁体   中英

Retrieve data from firebase using unique id

Actually I retrieve data from firebase with marker click listener. Each data has unique id. 护理医疗中心的标记

So when I click care Medical Center in then it goes to another window and another window is given below: 医院信息的另一个窗口

But when i click sust medical then it goes to same window and same data.

Actually main this is same data. How can i retrieve the different data for another marker.

Hare is my firebase: 在此处输入图片说明

Here is my code:

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

        hosp_name=(TextView) findViewById(R.id.hosp_name);
        hosp_email=(TextView) findViewById(R.id.hosp_email);
        hosp_num=(TextView) findViewById(R.id.hosp_number);

        FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
        DatabaseReference databaseReference1=firebaseDatabase.getReference("H_Information");

        databaseReference1.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
                {

                    hosp_name.setText(dataSnapshot1.child("hname").getValue().toString());
                    hosp_email.setText(dataSnapshot1.child("hemail").getValue().toString());
                    hosp_num.setText(dataSnapshot1.child("hmobile").getValue().toString());


                }
            }

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

            }
        });

    }
}

You need to add a query to retrieve the correct data, for example you can use the equalTo() :

        databaseReference1.orderByChild("hname").equalTo("sust medical").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
                {

                    hosp_name.setText(dataSnapshot1.child("hname").getValue().toString());
                    hosp_email.setText(dataSnapshot1.child("hemail").getValue().toString());
                    hosp_num.setText(dataSnapshot1.child("hmobile").getValue().toString());


                }
            }

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

            }
        });

When you click on sust medical, then take the name and pass it to as an argument in equalTo()

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