How to retrieve data from specific node from firebase in android - android

I am trying to retrieve phonenumbers joined under CircleMembers of current user but datasnapshot returning null.Please help me out.Which is proper way to retrieve members under mycircle.
My firebase database is below
Here is my code.
userReference = FirebaseDatabase.getInstance().getReference().child( "Users" );
databaseReference = FirebaseDatabase.getInstance().getReference().child( "Users").child( user.getUid() ).child( "CircleMembers" );
databaseReference.addListenerForSingleValueEvent( new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
nameList.clear();
if(dataSnapshot.exists()){
for(DataSnapshot dss:dataSnapshot.getChildren()){
String circlememberID = dss.child( "CircleMembers" ).getValue(String.class);
userReference.child( circlememberID )
.addListenerForSingleValueEvent( new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
phone = dataSnapshot.getValue( Phone.class );
//fetching phone numbers under Circlemembers (to which circle current user is joined)
nameList.add( phone);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText( getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT ).show();
}
} );
adapter = new MembersAdaptor(nameList,getApplicationContext());
recyclerView.setAdapter( adapter );
adapter.notifyDataSetChanged();
}
}
else{
Toast.makeText( getApplicationContext(), "No circle", Toast.LENGTH_SHORT ).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
} );

To get all those phone numbers that exist under the CircleMemebers node, please use the following code:
String phoneNo = FirebaseAuth.getInstance().getCurrentUser().getPhoneNumber();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference circleMemebersRef = rootRef.child("Users").child(phoneNo).child("CircleMemebers");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String phoneNumber = ds.getValue(String.class);
Log.d(TAG, phoneNumber);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
circleMemebersRef.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be:
+919212143422
+615325453454

Give this a trial. I think this is the way your code should go:
userReference = FirebaseDatabase.getInstance().getReference().child( "Users");
//Changed databaseReference to userCircleMembersRef for easier understanding of nodes
userCircleMembersRef =
userReference .child( user.getPhoneNumber()).child( "CircleMembers" );
userCircleMembersRef.addListenerForSingleValueEvent( new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
nameList.clear();
if(dataSnapshot.exists()){
for(DataSnapshot dss:dataSnapshot.getChildren()){
Phone phone = dss.child( user.getUid()).getValue( Phone.class );
//fetching phone numbers under Circlemembers (to which circle current user is joined)
nameList.add( phone);
}
adapter = new MembersAdaptor(nameList,getApplicationContext());
recyclerView.setAdapter( adapter );
adapter.notifyDataSetChanged();
}else{
Toast.makeText( getApplicationContext(), "No circle", Toast.LENGTH_SHORT ).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText( getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}

Related

Read child value from firebase

i want to get only one data from firebase .made lot of efforts but could not be successful
the structure of the data of my firebase is as follows
and trying something like this in my code
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("users").child("userID");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.getValue(String.class);
//do what you want with the email
setDisplayName.setText(email);
}
#Override
public void onCancelled(DatabaseError databaseError) {
setDisplayName.setText("data not get" + databaseError );
}
});
If you want to get only the email you can do something like this :
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("users").child("userID");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("email").getValue().toString();
//do what you want with the email
setDisplayName.setText(email);
}
#Override
public void onCancelled(DatabaseError databaseError) {
setDisplayName.setText("data not get" + databaseError );
}
});
If you dont want to load the other data, you can directly query the email like this :
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("users").child("userID").child("email");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.getValue().toString();
//do what you want with the email
setDisplayName.setText(email);
}
#Override
public void onCancelled(DatabaseError databaseError) {
setDisplayName.setText("data not get" + databaseError );
}
});

How to get specific child data from firebase

I have need to access a specific child from node, i have only "available key" of the specific child not complete path.
Since you know the key of the child you're looking for, you can query on that:
DatabaseReference schoolsRef = FirebaseDatabase.getInstance().getReference().child("School");
Query query = schoolsRef.orderByKey().equalTo("MZ3bW5kLJAQorgnZbYiTaOoWWSG2");
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
String email = userSnapshot.child("email").getValue(String.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
Note that you can't use orderByChild("id") as explained in my answer
here: Firebase Query Double Nested.
Just add this code:
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(firebaseUser.getUid();
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("Email").getValue().toString();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
try this :-
DatabaseReference mUserNameReference = FirebaseDatabase.getInstance().getReference("School").child("id").child("id");
mUserNameReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("email").getValue().toString());
//get all the key value like this
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
If you have key for that child but not complete path first get key of parent node in list like below I did.
Arraylist<String> keyList =new Arraylist();
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(firebaseUser.getUid();
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
String value= childSnapshot.getValue(String.class);
keyList.add(key);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
now use for loop or foreach loop to check if that node contains available child key.
for(String key:keyList){
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(key).orderByChild("id").equal(availableKey);
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
Object object= childSnapshot.getValue(Object.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
hope this will help you..

use two different adapters - android -fragment

I have an if and else block. if it conforms to the if condition, it will list the data in the database as imageAdapter. anasayfaAdapter will be used if elsee comes into it.
if(kisiIdd.equals(userId))
{
adapterr = new imageAdapter(getActivity(), kullaniciList);
recyclerView.setAdapter(adapterr);
adapterr.setOnItemClickListener(urunlerimFragment.this);
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
kullaniciList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Kullanici kullaniciModel = postSnapshot.getValue(Kullanici.class);
String id=kullaniciModel.getKisiId().toString();
kullaniciList.add(kullaniciModel);
kullaniciModel.setKey(postSnapshot.getKey());
}
adapterr.notifyDataSetChanged();
progressCircle.setVisibility(View.INVISIBLE);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
progressCircle.setVisibility(View.INVISIBLE);
}
});
}
else
{
anasayfaAdapter = new anasayfaAdapter(getActivity(), kullaniciList);
recyclerView.setAdapter(anasayfaAdapter);
anasayfaAdapter.setOnItemClickListener(urunlerimFragment.this);
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
kullaniciList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Kullanici kullaniciModel = postSnapshot.getValue(Kullanici.class);
String id=kullaniciModel.getKisiId().toString();
kullaniciList.add(kullaniciModel);
kullaniciModel.setKey(postSnapshot.getKey());
}
anasayfaAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
if the data in the database from the database if the imageAdapter to use to list. In the Else block, it will use the anasayfaAdapter adapter. Why would an algorithm fit?
but it does not happen. How do you have a suggestion?

Firebase Query get values

I have a list of Child-Data and wants to have a certain value.
My Firebase structure:
Now, I only have the Child "Text" and the currendUser "0zwtyRwgnogC8Qk91AtHj7amZb63"
So how can I get the values of "text and "user_id"?
My source code:
mDatabaseText = FirebaseDatabase.getInstance().getReference().child("Text");
query = mDatabaseText.startAt(mCurrentUser.getUid());
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot textSnapshot : dataSnapshot.getChildren()) {
/* String text = textSnapshot.getKey();
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); */
TextModel textModel = chatSnapshot.getValue(TextModel.class);
String t = textModel.getText();
Log.i("!!!!!!!text", t);
Toast.makeText(getActivity(), t, Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But this code doesn't work, why?
I used the query startAt.
Please use this code:
FirebaseDatabase rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference currentUserIdRef = rootRef.child("Text").child(currentUserId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String userId = (String) ds.getKey();
DatabaseReference userIdRef = rootRef.child("Text").child(currentUserId).child(userId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String user_id = (String) dataSnapshot.child("user_id").getValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
};
userIdRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
};
currentUserIdRef.addListenerForSingleValueEvent(eventListener);
In which currentUserId is the variable that you say you already have it.
Hope it helps.

get Firebase nodes by children

If I have a database that is set up like so
-users
-uId0
-Name: "Bill"
-uId1
-Name: "Bob"
-uId2
-Name: "Dave"
How do I get the uId elements if there name child starts with B.
The query should return somthing like this?
-uId0
-Name: "Bill"
-uId1
-Name: "Bob"
Thanks!!
DatabaseReference myReference = FirebaseDatabase.getInstance().getReference();
myReference.child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//looping all your users
for (DataSnapshot messageSnapshot:dataSnapshot.getChildren())
{
name = (String)messageSnapshot.child("Name").getValue();
if (name.toLowerCase().contains("b")) {
//u can save your name with 'b' string here
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To achieve this, please use this code:
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users");
usersRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String uId = (String) ds.getKey();
DatabaseReference uIdRef = FirebaseDatabase.getInstance().getReference().child("users").child(uId);
uIdRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = (String) dataSnapshot.child("Name").getValue();
if (name.matches("^[B].*$")) {
System.out.println(name);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});

Categories

Resources