My firebase database:
I want to get post id from post element.
I know authorId, createdDate, createdDateText and title, but I want to get post id.
How can I do that?
Please try with firebase query like bellow,
Query query = FirebaseDatabase.getInstance().getReference().child("posts");
query.orderByChild("title").equalTo("test");//Here replace title with your key which you want and replace test with value throw which search
//query.orderByChild("title").equalTo("test").limitToFirst(1); //If you want to only one value
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//you get here the list of post from DB
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String postId = child.getKey(); //Post Id
Logger.print("postId");
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
try final String postId = getRef(position).getKey();
Related
I'm new to Firebase I wanted to know is there a way to only get the value of first child and insert it into my spinner in android. Ex:
Just like the image shown I wanted to retrieve only the name "slot x" instead of getting all the values of it. Is it possible to do so?
Sure thing, you can order and limit the number of child nodes retrieved with something like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = rootRef.child("path to your parent node");
Query query = myRef.orderByKey().limiToFirst(1);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String key = childSnapshot.getKey(); // "slot 1"
String status = childSnapshot.child("status").getValue(String.class); // "occupied"
...
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
I have developed an admin side application from where I will be adding the email id, pin, city and country data to the firebase realtime database. Then I will be providing the email and pin to the customer. So my simple target is that when the customer logins, the data of city and country should be displayed on his application. To do this I have to search the database for that particular customer's node which can be done through the email id which he puts during login.
I tried my best but everytime null is returned. Please have a look at my code and help me out.
void searchQuery(String email){
DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
Query query = mFirebaseDatabaseReference.child("arvi-admin").orderByChild("mailId").equalTo(email);
query.addValueEventListener(valueEventListener);
}
ValueEventListener valueEventListener = new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
try {
DataModel obj = dataSnapshot.getValue(DataModel.class);
logData(""+obj.getCity()); //the values are null
}catch (Exception e){
Log.d("##","Error");
e.printStackTrace();
}
}
#Override
public void onCancelled(DatabaseError databaseError){
}
};
Firebase Database Image
To solve this, please change the following line of code:
Query query = mFirebaseDatabaseReference.child("arvi-admin").orderByChild("mailId").equalTo(email);
to
Query query = mFirebaseDatabaseReference.orderByChild("mailId").equalTo(email);
As you can see, I have removed the call to .child("arvi-admin") because the root is not a child and should not be added in your reference.
Edit:
Query query = mFirebaseDatabaseReference.orderByChild("mailId").equalTo(email);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
DataModel obj = ds.getValue(DataModel.class);
Log.d(TAG, obj.getCity());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
I have integrated Google sign-in in my app and i am pushing some data in to fire base including user U-id.I had researched a lot and didn't get anything the problem i am facing that i want to fetch Particular data for eg If User A sign-in and push 5 data and Then User B sign-in and push 3 data.I want a query like if User A sing-in Again it will get his 5 data only and not the data which is pushed by User B.Thanks in Advance :)
By using this it fetch all the data from firebase:
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
FirebaseModel firebasemodel =
data.getValue(FirebaseModel.class);
firebasemodels.add(firebasemodel);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I have tried all that .child .orderby and .equalTo but did'nt work
My Structure is Like:
My FireBase Structure
You also need a reference to the data, which isn't included in your code block. So something along the lines of (this should be before this):
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("notepad-secure/notepad");
Firstly add a user_id key in your child data, it will look like below :
id:"",
note:"",
title:"",
user:""
user_id:"{id from which user have you uploaded data}"
And than you can call below function with specific user data like
below Note : "user_id" = id from which user have you uploaded data
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("notepad").orderByChild("id").equalTo("user_id");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// dataSnapshot is the "notepad" node with all children with id
for (DataSnapshot notepad: dataSnapshot.getChildren()) {
// do something with the individual "notepad_data"
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To Fetch Particular values from the firebase,try this code
FirebaseDatabase database;
database.getReference().child("notepad-secure").orderByChild("id").equalTo(user).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
User userdet =childSnapshot.getValue(yourclass.class);
String note=userdet.note;
//Here you will get the string values what you want to fetch
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
First U have to differentiate.U have to implement Firebase Authentication the u can get Firebase UID of every USER.
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
Then store seperatly in new node.
i.e Take Firebase Database Instance and
databaserefernace.child("Data").Child("userID);
When your add a user data to Firebase database, you can add it using the specific uid provided by the FirebaseAuth object like this:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Assuming you that your database strucure look like this: Firebase root -> notepad-secure -> notepad, to retrieve data, you can use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference notepadRef = rootRef.child("notepad-secure").child("notepad").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String id = dataSnapshot.child("id").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String title = dataSnapshot.child("title").getValue(String.class);
String user = dataSnapshot.child("user").getValue(String.class);
Log.d("TAG", id + " / " + note + " / " + title + " / " + user);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
notepadRef.addListenerForSingleValueEvent(eventListener);
I have a problem several days ago and I don't find a solution.
I´m trying search in my firebase database by name but the result is always null.
The database is the next;
The code that I use is the next;
final String nombre = edtNombreJugador.getText().toString().trim().toUpperCase();
mDatabase.child("user").child("personalData").orderByChild("name").equalTo(nombre);
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
String title = (String) singleSnapshot.child("name").getValue(String.class);
System.out.println("TITLE: "+title);
}
Title always return null in my case.
Can somebody help me please? I don't know what I'm doing bad...
Right now you're querying the database path /user/personalData. And then for each child node under there, you're comparing the name property to the value the user entered. So /user/personalData/***/name.
That doesn't match the path in the database, which is /user/***/personalData/name. To query that you do:
Query query = mDatabase.child("user").orderByChild("personalData/name").equalTo(nombre);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {...
Im trying too with this code (more clear for me) for return all names in database but the result is null too:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("/users/");
mDatabase.child("personalData");
final UsersData[] user = {null};
ValueEventListener postListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String title = (String)
postSnapshot.child("name").getValue(String.class);
System.out.println("TITLE: "+title);
}
}
#Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("The read failed: " ,firebaseError.getMessage());
}
};
mDatabase.addValueEventListener(postListener);
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
mValueView = (TextView) findViewById(R.id.textView);
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
com.google.firebase.database.Query query = mRef.child("Users").orderByChild("title").equalTo(name);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Map<String,Object> map = (Map<String, Object>) dataSnapshot.getValue();
//String Title = (String) map.get("title");
String Title = dataSnapshot.child("title").getValue().toString();
mValueView.setText(Title);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to show object title is same name value.
This is the Firebase database:
If you want to search title value only one time, without listening for updates, you can simply use :
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}});
Then if you get a reference to 'Users' you can do some logic with iteration, for example :
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
String title = (String) singleSnapshot.child("title").getValue();
//do your logic
}
There are two problems in your code:
you're specifying the child node Users twice
a query results a list of results, which your onDataChange doesn't handle
specifying the child node Users twice
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
// ^^^^^
Query query = mRef.child("Users").orderByChild("title").equalTo(name);
// ^^^^^
Easily fixed:
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/");
Query query = mRef.child("Users").orderByChild("title").equalTo(name);
I'm not sure why you use getReferenceFromUrl() to begin with. For most applications, this accomplishes the same is simpler:
mRef = FirebaseDatabase.getInstance().getReference();
a query results a list of results
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
System.out.println(snapshot.getKey());
System.out.println(snapshot.child("title").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});