I wrote this code to get the values of a user who typed his username and i want to check his password :
MyDatabase.child("USERS").orderByChild("username").equalTo(Username.getText().toString())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child :dataSnapshot.getChildren()){
Log.i("Password",child.getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
now i want to access the password field and check if the user entered the correct password or not .
There are 2 ways to achieve that. The first one is not to let the user leave an empty EditText when creating the account. And second is to verify if the dataSnapshot.exists(). Hope it helps!
This will work:
MyDatabase.child("USERS")
.orderByChild("username").equalTo(Username.getText().toString())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child :dataSnapshot.getChildren()){
Log.i("Password",child.child("password").getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
The changes:
child.child("password").getValue()
don't ignore onCancelled()
Related
i faced an issue like user entered enrollment number and verify and read all data from that and set the value to text view
If user enter enrollment 66 then verify that enrollment and read all that data from 66 and set value of text view to marks of subjects.what i need to do if i want to solve Above problem?
I tried:
reff= FirebaseDatabase.getInstance().getReference().child("Marks");
reff.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String mm=dataSnapshot.child("mcad").getValue().toString();
String jm=dataSnapshot.child("java").getValue().toString();
String nm=dataSnapshot.child("nma").getValue().toString();
txtmcadmarks.setText(nm);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
First, your Database Reference returns you a list of data so you need to receive your data as a list.
Second, if you want to filter your data base on enrollment then add a query in your database reference.
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query query = databaseReference.child("Marks").orderByChild("enrollment").equalTo(66);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data: dataSnapshot.getChildren()){
String mm=data.child("mcad").getValue().toString();
String jm=data.child("java").getValue().toString();
String nm=data.child("nma").getValue().toString();
txtmcadmarks.setText(nm);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Here is a screenshot of my database
Firebase Database Image
I got the user key ID by writing the following code
userID=Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
I need to search for the child in that key ROLE to check if it's STUDENT or INSTRUCTOR
The problem is all the solutions I've found it online require a parent node to search through , I don't know if this user is a student or instructor , so I can't just write
FirebaseDatabase.getInstance().getReference().child("students")
or
FirebaseDatabase.getInstance().getReference().child("instructors")
I've tried to access the key directly but with no luck
FirebaseDatabase.getInstance().getReference().child(userID);
So, how could this be implemented?
Check for both
dbRef = FirebaseDatabase.getInstance().getReference().child("students");
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild("userID"));
{
//isStudent = true;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
and
dbRef = FirebaseDatabase.getInstance().getReference().child("instructors");
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild("userID"));
{
//isInstructor = true;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The value of isStudent and isInstructor will help you find, whether the user was a student or an instructor. If user id is present only in either student or instructor then only one of the variables isStudent or isInstructor will be true. If both are false then user does not exist.
I want to read data offline for my users, but Persistance mode will make OnDataChange in ValueEventListener call twice. how can I prevent it. I need to use ValueEvent for my own purpose. someone help me please.
Update : I am making a friend suggestions list for users in my app, the suggested user must dont have any relationship with the current user. and this is my code ( I use Persistance for offline data as Firebase's suggestion)
valueEventListener_check_relationship=new ValueEventListener() {
#Override
public void onDataChange(#NonNull final DataSnapshot dataSnapshot_root) {
num_fiend_req=0;
num_sent_req=0;
update_badges(num_sent_req,tv_badges_sent_req);
update_badges(num_fiend_req,tv_badges_friend_req);
arrayList_suggestions.clear();
adapter_suggestions.notifyDataSetChanged();
GlobalConstants.ROOT_REF.child(GlobalConstants.CHILD_USERS)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
Object_User user_suggested=snapshot.getValue(Object_User.class);
if(!user_suggested.user_id.equals(GlobalConstants.getMyUID())){
if(!dataSnapshot_root.hasChild(user_suggested.user_id))
addToList(user_suggested);
else {
RemoveUserFromlist(user_suggested);
if (dataSnapshot_root.child(user_suggested.user_id)
.hasChild(GlobalConstants.CHILD_REQUESTING)){
String values=dataSnapshot_root.child(user_suggested.user_id)
.child(GlobalConstants.CHILD_REQUESTING)
.getValue().toString();
if (values.equalsIgnoreCase(GlobalConstants.VALUE_REQUESTING_SENDER))
num_sent_req++;
else num_fiend_req++;
}
update_badges(num_sent_req,tv_badges_sent_req);
update_badges(num_fiend_req,tv_badges_friend_req);
adapter_suggestions.notifyDataSetChanged();
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
GlobalConstants.ROOT_REF.child(GlobalConstants.CHILD_RELATIONSHIP)
.child(GlobalConstants.getMyUID()).keepSynced(true);
GlobalConstants.ROOT_REF.child(GlobalConstants.CHILD_RELATIONSHIP)
.child(GlobalConstants.getMyUID())
.addValueEventListener(valueEventListener_check_relationship);
I want to reach second child directly , first child name is random.
I want to reach kullaniciadi child;
https://i.hizliresim.com/dO58mp.png
try this:
DatabaseReference db=FirebaseDatabase.getInstance().getReference().child("Kullanicilar");
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
String kullaniciad=data.child("kullaniciadi").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here you have the datasnapshot at child Kullanicilar. Then you iterate inside of it and get the child kullaniciadi that is under the pushids
I have very simple database
TokenAdmin:
---xccsaccxvkkgmcdds :
---token: "cxcxzcfsdfdsfdsfs"
---uid: "xccsaccxvkkgmcdds"
---hjhbnghtfgdgdgdffg :
---token: "vhghtytyryyr"
---uid: "hjhbnghtfgdgdgdffg"
Now in my code I want to check and execute some code only if it find uid equal to "hjhbnghtfgdgdgdffg"
Here is my code :
mDatabase.child("TokenAdmin").orderByChild("uid").equalTo(mycurrUser.getUID()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//say here toast "A"
} else {
//toast "B"
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// ...
}
});
Now I run the code with mycurrUser.getUID() value set to "xxx" which I don't have listed in the database. I expected the code output was toast B, but instead I always get toast A, which in my understanding, it has found the value in the db. How to fix this, where did I do wrong ?
You need to use something like this:
mDatabase.child("TokenAdmin").orderByChild("uid").equalTo(mycurrUser.getUID()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
//Toast A
} else {
//Toast B
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
Hope it helps.
I'm confused why you're using a query to load the user. You seem to store the token under they UID key already, so this would work:
mDatabase.child("TokenAdmin").child(mycurrUser.getUID()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
... user exists
} else {
... user node does not exist
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});