Search by name with firebase in android - android

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

Related

Comments not in correct order by their names and timestamp firebase database

The comment section of my application is not working properly, the childs are mixing up by their names and the timestamp, the content inside of them it is in correct order, here are all of my code inside Pastebin links (if necessary to see the whole code), also I provided the chunks I think that the problem is in.
1) CommentAdapter.java Activity (https://pastebin.com/m7LHDUDF)
Take note of this block of code:
private void getHandleName(final ViewHolder viewHolder) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Log.d(TAG, "getHandleName: checking comment userID" + viewHolder.comment.getUser_id());
Query query;
query = reference
.child("data")
.child("-Kxzyb5JsUPhsMQAb84X")
.child("users")
.orderByChild("user_id")
.equalTo(viewHolder.comment.getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
viewHolder.handleName.setText(singleSnapshot.getValue(User.class).getHandlename());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
2) CommentRecylerViewAdapter (https://pastebin.com/Tb7L9EVD)
private void getHandleName(final CommentViewHolder viewHolder, Comment comment) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Log.d(TAG, "getHandleName: checking comment userID" + comment.getUser_id());
Query query = reference
.child("data")
.child("-Kxzyb5JsUPhsMQAb84X")
.child("users")
.orderByChild("user_id")
.equalTo(comment.getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
viewHolder.handleName.setText(singleSnapshot.getValue(User.class).getHandlename());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
3)Chunk of code for adding the comment:
` private void addComment() {
if (commentText.getText().toString().isEmpty()) {
Toast.makeText(ViewPostActivity.this, "Please enter your comment", Toast.LENGTH_SHORT).show();
} else {
String currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
String commentID = reference.push().getKey();
Comment comment = new Comment();
comment.setCaption(commentText.getText().toString());
comment.setDate_created(System.currentTimeMillis());
comment.setUser_id(currentUserID);
reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("comments").child(postID).child(commentID).setValue(comment);
setNumComment();
setNumPointCurrentUser();
setNumPointUser();
setNumPointPost();
}
}`
4) **And Finally, the comment model Activity** which I think is ok(https://pastebin.com/VaYV3Gv3)
Here is a screenshot of my database:
-Kxzyb5JsUPhsMQAb84X is the root of my database
comments is where all the comments are store per post
-LSv6lXZml-Cf0GX3i5q randomly generated child that stores the other content such as Timestamps,user id and content
Here is the screenshot of my phone showing the incorrect order of the comments:

Firebase query in tree with depth and multiple children

I have a problem regarding querying the firebase database using a value and getting the specific node. My schema is shown here:
In my schema 'workLocations' belongs to an 'excavationWorks', and 'excavationWorks' belongs to an 'excavationLists'.
That means that the path to a specific workLocation is excavationLists/excavationWorks/workLocations/(specific workLocation)
The problem that I have is that I want to query the workLocations node by the location value (let's say London) and get the parent node (the red circled key which is the key of the specific workLocation).
I have search and read many posts but I haven't managed to make it work.
My code looks like this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("workLocations").orderByChild("location").equalTo("London");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot workLoc : dataSnapshot.getChildren()) {
// do something with the individual "issues"
Log.d(TAG, workLoc.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thank you
To achieve this, you need to query your database twice like this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
DatabaseReference workLocationsRef = reference
.child("excavationLists")
.child("excavationWorks")
.child("workLocations");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String key = ds.getKey();
Query query = workLocationsRef.child(key).orderByChild("location").equalTo("London");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
String description = snapshot.child("description").getValue(String.class);
Log.d("description", description);
String partentKey = snapshot.getRef().getParent().getKey();
Log.d("partentKey", partentKey);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
workLocationsRef.addListenerForSingleValueEvent(valueEventListener);

how to search a data from firebase for android

I want to search a condition ,if student studied at 'swe' department then i will show name and id for every student who are studied at swe department.
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("user");
databaseReference.orderByChild("department").equalTo("swe").addValueEventListener(new com.google.firebase.database.ValueEventListener() {
#Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
String str = dataSnapshot.child("department").getValue(String.class);
Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
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.
Your current code fails to handle the fact that the result is a list. To solve this, loop over dataSnapshot.getChildren():
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("user");
databaseReference.orderByChild("department").equalTo("swe").addValueEventListener(new com.google.firebase.database.ValueEventListener() {
#Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String str = dataSnapshot.child("department").getValue(String.class);
Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
}
}

How to search data in Firebase?

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

Retrieving specific node from firebase database in android

I am trying to use Firebase in my android application. I need to get an object, i am searching with its email (attribute in the object).
this is my code of search:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Remorqueur");
Query queryRef=myRef.orderByChild("email").equalTo(ed_mail.getText().toString());
If the object exists in my database I need to get the whole object back! how can I do it?
should be able to do something like following
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
MyPojo app = childSnapshot.getValue(MyPojo.class);
}
Try this Please
DatabaseReference dbRef = firebaseDatabase.getReference("Remorqueur");
Query query = profileDbRef
.orderByChild("email")
.equalTo(ed_mail.getText().toString());
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Log.d(TAG, dataSnapshot1.getKey() + " " + dataSnapshot1.getValue(Profile.class)
.toString());
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Categories

Resources