问题
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ListView listView = (ListView) rootView.findViewById(R.id.usersList);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, al);
for(DataSnapshot ds : dataSnapshot.getChildren()) {
name = ds.child("Name").getValue().toString();
list_user_id = ();
// Log.d("TAG", name);
al.add(name);
// String phonenumber =
}
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(),"Please Check Your Internet Connection",Toast.LENGTH_SHORT).show();
}
};
I want the phone number to be fetched in the list_user_id... for fetching current user phone number i can just use getPhoneNumber method but im not sure on how too fetch the Number of the user im chatting with
回答1:
Try this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ref.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String phonenumber=datas.child("phonenumber").getValue().toString();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Since you want to get the phone number of the person you are chatting with, then logically you would click on that person's name to start the chat, then you can use the query:
ref.orderByChild("name").equalTo(Nameyouclicked).addValueEventListener(...){..}
This way you would have the name, then you can get the userid or anything related to that user.
来源:https://stackoverflow.com/questions/48679560/which-method-should-i-use-to-fetch-the-phone-number-of-a-user-which-they-provide