Firebase get a value under unknown push key - android

How can I get that value while I know nothing about the push key?
+users
+9JZTuGUzc8bx7FLrwResWmp8L583
+anon:
+email:
+fid: <- i want to get this id without knowing push key (9JZTuGUzc8bx7FLrwResWmp8L583 )
+username:
Currently, I am trying with a null response:
FirebaseDatabase.getInstance().getReference().child("users").orderByChild("anon").equalTo(getIntent().getStringExtra(EXTRA_POST_USERNAME))
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user information
if(dataSnapshot.exists()){
User user = dataSnapshot.getValue(User.class);
fidanon = user.fid;}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});

final Query query = FirebaseDatabase.getInstance().getReference().child("users").orderByChild("anon").equalTo(getIntent().getStringExtra(EXTRA_POST_USERNAME));
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
final String fid = dataSnapshot.child("fid").getValue().toString();
Toast.makeText(getActivity(), fid, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Use this,will solve your problem.

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

Firebase query 2 same methods one is working,other not

I got 2 same methods in one I declare string in Query and on another I declare it thru variable, but when I run apk the one I declare string inside Query works but the other does not.
First method:
private void randomtest() {
Query query = FirebaseDatabase.getInstance().getReference("users").orderByChild("username").equalTo("darkarcher5");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot users : dataSnapshot.getChildren()) {
User user = users.getValue(User.class);
Log.e("username", user.getUsername());
Log.e("password", user.getPassword());
userList.add(user);
}
txtViewPass.setText(userList.get(0).getUsername());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Second method:
private void neRandomTest() {
if (username != null) {
Query query = FirebaseDatabase.getInstance().getReference("user").orderByChild("username").equalTo(username);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot users : dataSnapshot.getChildren()) {
User user = users.getValue(User.class);
Log.e("username", user.getUsername());
Log.e("password", user.getPassword());
userList.add(user);
}
txtViewPass.setText(userList.get(0).getUsername());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Error given by second method: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
In your first method, your query is referencing on the "users" node while on your second method your query is referencing on the "user" node. If I am correct you are trying to connect to "user" node in second method which is not available in the firebase database. That's why indexoutofbounds.
Try using this:
private void neRandomTest() {
if (username != null) {
//Query query = FirebaseDatabase.getInstance().getReference("user").orderByChild("username").equalTo(username);
Query query = FirebaseDatabase.getInstance().getReference("users").orderByChild("username").equalTo(username);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot users : dataSnapshot.getChildren()) {
User user = users.getValue(User.class);
Log.e("username", user.getUsername());
Log.e("password", user.getPassword());
userList.add(user);
}
txtViewPass.setText(userList.get(0).getUsername());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}

Retrieve first child from a parent in Firebase

I am working on an Android application. I want the first child (first UID) of the parent 'support' and store it in a String variable. How do I get the value of the first UID from the list?
I tried one approach. It doesn't work though.
#Override
public void onDataChange(DataSnapshot dataSnapshot1) {
if (dataSnapshot1.exists()) {
String futureUID = "";
for(DataSnapshot futureUIDdatasnapshot:dataSnapshot1.getChildren() ){
futureUID = futureUIDdatasnapshot.getKey();
break;
}
/*Getting the first UID from the list of UID's in queue in 'future'*/
futureUID = dataSnapshot1.getChildren().iterator().next().getKey();
/*Moving a card from 'future' to 'serving'*/
societyServiceUIDReference.child(FIREBASE_CHILD_SERVING).child(futureUID).setValue(FIREBASE_ACCEPTED);
/*Removing the UID from 'future' after it is placed in 'serving'*/
societyServiceUIDReference.child(FIREBASE_CHILD_FUTURE).child(futureUID).removeValue();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
NOTE: 'futureUID' is the UID I want
Try the following:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("support");
Query queryUid=ref.orderByKey().limitToFirst(1);
queryUid.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
String key=datas.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
.orderByKey().limitToFirst(n) is what does the trick. It orders the query results by key and returns only the first n results; in this case 1
mDatabase.getChild("support").orderByKey().limitToFirst(1)
.addListenerForSingleValueEvent(new ValueEventListener () {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot supportItem: dataSnapshot.getChildren()) {
String futureUID =supportItem.getKey();
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//Catch your error here
}
});
see Work with Lists of Data on Android

Firebase database Object.toString()' on a null object reference error

So i have a viewholder which has the name of the user and Under it i wanna put the last message that this user received , i have a query that gives me the last node in the database there is my code :
Query lastQuery = allDb.child("ReceivedMessages").child(mCurrent_user_id).child(list_user_id).orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String message = dataSnapshot.child("message").getValue().toString();
friendViewHolder.lastMessage.setText(message);
}
#Override
public void onCancelled(DatabaseError databaseError) {
//Handle possible errors.
}
});
and this is my database structure (also i'm sure that the referance is correct because whenever i assign all of the data to a textView it gives me the last node plus the message and date etc .. but i only want the message )
and this is my error
I hope this will do the job.
Query lastQuery = allDb.child("ReceivedMessages").child(mCurrent_user_id).child(list_user_id).orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnaphot.getChildren()) {
String message = ds.child("message").getValue().toString();
friendViewHolder.lastMessage.setText(message);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//Handle possible errors.
}
});

Search Whole Firebase Database For Single Value

I am using firebase auth ui in order to create users with mail and password but after the registration I forward them to a new activity in order to choose I username which needs to be unique. My idea was to search the whole DB in order to see if that username exists already or not. Here is how I am trying to do this but no luck till now:
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("UserName", "Click: ");
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("USERS");
mDatabase.orderByChild("username").equalTo("name1").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
//Here you can get all data
Log.d("UserName", "onDataChange: "+snapshot.toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
I have also tried this one:
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("UserName", "Click: ");
DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
Query query = mFirebaseDatabaseReference.child("USERS").orderByChild("username").equalTo("name1");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//TODO get the data here
Log.d("UserName", "onDataChange: " + dataSnapshot.toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
query.addValueEventListener(valueEventListener);
}
});
The only reason your code is not working is because "username" is not a direct child of the USERS table.
Your direct children will have random ids , which you can iterate trough with ChildEventListener like this:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("USERS");
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
User user = dataSnapshot.getValue(User.class); // pojo
if(user.getUsername().equals("name1") {
// do something
}
}
...
)

Categories

Resources