I successfully get the username by searching the subject which is saved under username in the tlocation. But what if the subject totally not exist under all username so how I am gonna detect whether the subject exist in the tlocation? If you got any idea please share it to me thank you.
here's my code
tref.orderByChild("subject").equalTo(ssubject).
addValueEventListener(new ValueEventListener() {
//ssubject is user selected subject
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (final DataSnapshot u: dataSnapshot.getChildren()) {
if(u.child("subject").getValue().equals(ssubject)){
// do some code here
}else{
searchuserlist.setEmptyView(findViewById(R.id.stv));
}
here's my database structure
This is how you can verify if the subject exists:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("tlocation").child("rexyou0831");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child("subject").exists()) {
//do something
} else {
//do something else
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userRef.addListenerForSingleValueEvent(eventListener);
I have used rexyou0831 as an example. Instead of rexyou0831 you can use the userName variable.
Related
Hello I'm trying to retrieve the data from a realtime firebase field but it returns null. I checked with textView52.setText (firebaseUser.getEmail ()); to see if it was really connected and it works. the problem is in recovering a value from the database which in this case is the field "name" could someone help me?
am i doing something wrong?
any help is welcome.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userUid = user.getUid();
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.child("name").getValue(String.class);
textView52.setText(data);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
try
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userUid = user.getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("Users").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.child("name").getValue();
textView52.setText(data);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});}
UPDATED!
check those lines
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {...}
Basically you try to call in Users another child called User, to understand very easy, replace the of that user here
ref.child("use_that_key_here").addListenerForSingleValueEvent(new ValueEventListener() {...}
Is not a solution, you cannot hard code every key, but with this you should see the data
try something like that:
// ref to db
DatabaseReference ref= FirebaseDatabase.getInstance();
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//debug this par and check if you can see your user
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
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:
usersRef= myRef.child("users");
usersRef.orderByChild("name").equalTo("yair").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user=new User();
user.setName(dataSnapshot.getValue(User.class).getName());
user.setEmail(dataSnapshot.getValue(User.class).getEmail());
user.setId(dataSnapshot.getValue(User.class).getId());
user.setPhone_num(dataSnapshot.getValue(User.class).getPhone_num());
my firebase looks like that:
users
Y6NKciGllTgkLXlMj9YLlAFuw522
email:
"yk.yair#gmail.com"
id:
"Y6NKciGllTgkLXlMj9YLlAFuw522"
name:
"yair"
phone_num:
"050-7777151"
kfby10Wy16Rx12v7Mx0sksqUTE72
email:
"aaa"
id:
"kfby10Wy16Rx12v7Mx0sksqUTE72"
name:
"aaa"
phone_num:
"(050)7777151"
it seems that the user always stays null;
can anyone help?
To solve this, please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String email = ds.child("email").getValue(String.class);
User user = new User();
user.setEmail(email);
//and so on
Log.d("TAG", email);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
You output will be:
yk.yair#gmail.com
Another approach will be just to change the DatabaseReference and use the same code like this:
DatabaseReference usersRef = rootRef.child("users").child(userId);
In which userId is the unique id generated by the push() method.
Hope it helps.
try this:
mFirebaseDatabase = FirebaseDatabase.getInstance();
mFirebaseDatabase.getReference().child("users").orderByChild("name").equalTo("yair").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() == null){
Log.i("IS NULL", "IS NULL");
} else{
User user = dataSnapshot.getValue(User.class);
Log.i("user name", user.getName());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
and make sure that your database is structured like this:
enter image description here
I want to get the value of 'caste' key from the JSON tree in Firebase Realtime Database.See this image
I have the user's unique ID and all my auth and user objects are in place. How do I get a reference to only the 'caste' key into a String variable?
Thanks,
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("project-android-536f3");
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
String caste = (String) messageSnapshot.child("caste").getValue();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I figured it out. I never provided the unique User ID so it never got data from the database.
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference(firebaseUser.getUid()).child("caste");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
caste = dataSnapshot.getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to extract all the user's username (highlighted blue) who are online (highlighted red) from a firebase database to a list view.
Just tested this, and it works!
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference("drivers");
Query query = reference.orderByChild("online").equalTo("true");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, dataSnapshot.toString());
}
public void onCancelled(DatabaseError databaseError) { }
});
I found the answer to the question, thanks to A. Omar and Frank van Puffelen for the help:
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://<your-app>.firebaseio.com/drivers/");
ref.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot driverSnapshot: dataSnapshot.getChildren())
{
if(driverSnapshot.child("online").getValue().toString()=="true"){
Log.d(TAG, driverSnapshot.getKey() + " - " + driverSnapshot.child("online").getValue().toString());
}
}
}
public void onCancelled(FirebaseError firebaseError) {
}
});
this will print only the online users like:
2332424 - true