im trying to get the "type" value here (see the image)
and here is what im doing
firebaseAuth.signInWithEmailAndPassword(email , password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
DatabaseReference reference = firebaseDatabase.getReference("user type").child(firebaseAuth.getCurrentUser().getUid());
}
}
});
but i don't know what todo further.
Change it to
DatabaseReference reference = firebaseDatabase.getReference("usertype").child(firebaseAuth.getCurrentUser().getUid()).child("type");
i did this and it worked
firebaseAuth.signInWithEmailAndPassword(email , password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
DatabaseReference reference = firebaseDatabase.getReference("user type").child(firebaseAuth.getCurrentUser().getUid());
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String temp = snapshot.child("type").getValue().toString();
Log.d("hereIm","temp == "+temp);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
});
Related
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email=((EditText)findViewById(R.id.email)).getText().toString();
String password=((EditText)findViewById(R.id.password)).getText().toString();
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener( new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(SignUpActivity.this,"Hooray!",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(SignUpActivity.this,"Ohhno!",Toast.LENGTH_LONG).show();
}
}
});
}
});
This is my layout file snippet
I have also enabled email password authentication in firebase.
Just trying to make a phone authentication as per a tutorial i'm watching which I have written down the code as below, but i keep getting the error expression expected for 'User' - I am completely new to this so I imagine it's something simple I need help nonetheless! Thank you in advance!
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(#NonNull PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) {
FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(this, new OnCompleteListener<Credential.UserCredential>() {
#Override
public void onComplete(#NonNull Task<Credential.UserCredential> task) {
if(task.isSuccessful())
userIsLoggedIn();
}
private void userIsLoggedIn() {
User = FirebaseAuth.getInstance().getCurrentUser();
if(User != null){
startActivity(new Intent(getApplicationContext(), MainPageActivity.class));
finish();
return;
You have a class named "User" and you didn't define its variable here which showing java expression error . Actually you are looking for User of Firebase , FirebaseAuth has class FirebaseUser . So it should be below code to check current user .
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null){.....
Update (cause of new error) - You are implementing PhoneAuthCredential , but passing Username-password based Credential.UserCredential and implemeting wrong method.
Your signInWithPhoneAuthCredential void would be -
private void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) {
FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(this , new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
userIsLoggedIn();
} else {
//
}
}
});
}
And your attached code would be -
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(#NonNull PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
#Override
public void onVerificationFailed(#NonNull FirebaseException e) {
//TODO
}
#Override
public void onCodeSent(#NonNull String s, #NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
//TODO
}
};
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) {
FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(this , new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
userIsLoggedIn();
} else {
//
}
}
});
}
private void userIsLoggedIn() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
startActivity(new Intent(getApplicationContext(), MainPageActivity.class));
finish();
// return;
}
}
Please check the documentation.
Why data in the firebase are created this way and not in the normal way
this is my code
ProductRef.child(IdMesa_1).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
facturas.setValue(dataSnapshot.getValue().toString()).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task)
{
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
When you call dataSnapshot.getValue().toString(), you're converting a branch of your JSON tree into a string. When you then write that back to Firebase, it saves it as a string.
If you want to save it in the same structure as before, don't convert the data to a string. So:
ProductRef.child(IdMesa_1).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
facturas.setValue(dataSnapshot.getValue());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
I wish to delete specific user with phone number X from my DB. And insted my code deletes all of them.
Here is my DB:
Code:
private static void deleteUser(final Context context, final String phoneNumber) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users");
Query usersQuery = databaseReference.orderByChild("phone").equalTo(phoneNumber);
usersQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Log.d(TAG, "Deleted");
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}
});
}
Maybe it is not clear with my comment, so I am putting this as an answer, the full code that should not be having the problem you're facing, should look something like this:
usersQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()) {
ds.getRef().removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Log.d(TAG, "Deleted");
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}
});
That's because you're using a singleValueEventListener. If the query matches multiple children, it returns a list of all those children.
Even if there's only a single matches child, it's still a list of one. And since you're calling getRef() on that list, you get the key of the location where you ran the query.
I want to get Date of "Friends" very 1st child/child. For this, i wrote query as
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Friends").child("SQyOq80egYehjqx4sgiyeNcW8P02");
in the above query, i want to specifically get date of "SQyOq80egYehjqx4sgiyeNcW8P02". How can i do that? Above query not working.
i've previously write these values by this code:
final String currentDate = DateFormat.getDateInstance().format(new Date());
mFriendDatabase.child(mCurrent_user.getUid()).child(user_id).setValue(currentDate)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
mFriendDatabase.child(user_id).child(mCurrent_user.getUid()).setValue(currentDate)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
mFriendReqDatabase.child(mCurrent_user.getUid()).child(user_id).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
mFriendReqDatabase.child(user_id).child(mCurrent_user.getUid()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
mProfileSendReqBtn.setEnabled(true);
mCurrent_state = "friends";
mProfileSendReqBtn.setText("UnFriend");
mDeclineBtn.setVisibility(View.INVISIBLE);
mDeclineBtn.setEnabled(false);
}
});
}
});
}
});
}
});
}
Recycler code:
FirebaseRecyclerOptions<Friends> options =
new FirebaseRecyclerOptions.Builder<Friends>()
.setQuery(query, Friends.class)
.build();
Log.d("sdfsdfsdf", options.getSnapshots().toString());
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(options) {
#Override
public FriendsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_users_activity_layout, parent, false);
return new FriendsViewHolder(view);
}
#Override
protected void onBindViewHolder(final FriendsViewHolder friendsViewHolder, int position, Friends friends) {
// Log.d("sdfsdfdf", friends.getDate().toString());
friendsViewHolder.setDate(friends.getDate());
If you want to find some value using Query
Query applyQuery = ref.child("Friends").orderByChild("Wed5qPTC.....").equalTo("Value for Check");
applyQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap: dataSnapshot.getChildren()) {
//You will get your value in snap
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("Realtime", "onCancelled", databaseError.toException());
}
});
Assuming that SQyOq80egYehjqx4sgiyeNcW8P02 is user the id of the user that is logged in, to get value of wed5 ... 43v1, please use the following code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Friends").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String date = ds.getValue(String.class);
Log.d(TAG, date);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be:
Sept 29, 2018
If SQyOq80egYehjqx4sgiyeNcW8P02 is another random generated id, in this case please use the following reference:
DatabaseReference uidRef = rootRef.child("Friends").child("SQyOq80egYehjqx4sgiyeNcW8P02");