I am trying to get Data and comparing it to the input data but it doesn't do anything. Pls Help me!`btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Routes").child("route1").child("Students").child("driver");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String phoneNumber = et_phone.getText().toString();
String password = et_password.getText().toString();
String phone = dataSnapshot.child("Phone_No").getValue().toString();
String pass = dataSnapshot.child("Password").getValue().toString();
if(phone.equals(phoneNumber)){
if(pass.equals(password)){
startActivity(new Intent(Authentication.this, Home.class));
}else{
Toast.makeText(Authentication.this, "Worng Password!!!", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(Authentication.this, "Invalid PhoneNumber![enter image description here][1]", Toast.LENGTH_LONG).show();
}*/
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});`
Related
In this code I need to save the data in firebase database as (Registration Data1 then UID then push id)
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String uid = firebaseUser.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Registration
Data1").child(uid).push();
String sp1 = spinner.getSelectedItem().toString();
String sp2 = spinner2.getSelectedItem().toString();
String sp3 = spinner3.getSelectedItem().toString();
Data DBdata = new Data(sp1,sp2,sp3,uid);
databaseReference.setValue(DBdata);
//Toast.makeText(this,"Data Added",Toast.LENGTH_SHORT).show();'''
And in this code I need to retrieve the data in recyclerview
databaseReference = FirebaseDatabase.getInstance().getReference().child("Registration Data1");
mDBListener = databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot teacherSnapshot : dataSnapshot.getChildren()) {
Data DB = teacherSnapshot.getValue(Data.class);
DB.setKey(teacherSnapshot.getKey());
mTeachers.add(DB);
}
mAdapter.notifyDataSetChanged();
mProgressBar.setVisibility(View.GONE);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Public.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
mProgressBar.setVisibility(View.INVISIBLE);
}
});
The question is how to retrieve the data under push id without make .child(UID)?
I am trying to fetch the data of current user with PhoneNumber from firebase database but i am not able to fetch the data
My Application is PhoneAuthtication and i have qiven the number as a child and reference path is User What should i do to fetch the data
reference = FirebaseDatabase.getInstance().getReference("Users");
reference.child(user.getPhoneNumber()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String A = String.valueOf(snapshot.child("fullName").getValue());
String B = String.valueOf(snapshot.child("phone1").getValue());
String C = String.valueOf(snapshot.child("phone2").getValue());
String D = String.valueOf(snapshot.child("message").getValue());
Name.setText(A);
Phone1.setText(B);
Phone2.setText(C);
Message.setText(D);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
Show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ProfileActivity.this,HomeActivity.class);
startActivity(intent);
finish();
}
});
Using Firebase, I'm trying to display to the user people they have matched with. I already have valid data for testing this and I have already run tests with sample data to see if everything else works.
Now, when I use real data from Firebase, problems occur.
This is what I have for code:
public String username = "";
public String profileImage = "";
public String discussion = "";
private void FetchMatchInformation(final String key, final String choice) {
DatabaseReference matchDb = FirebaseDatabase.getInstance().getReference().child("answers").child(key);
matchDb.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String opposite = postSnapshot.getValue(String.class);
if(opposite.equals("agree") && choice.equals("disagree")) {
DatabaseReference found = FirebaseDatabase.getInstance().getReference().child("users").child(postSnapshot.getKey());
found.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
if(postSnapshot.getKey().equals("username")) {
username = postSnapshot.getValue(String.class);
}
if(postSnapshot.getKey().equals("providerId")) {
String provider = postSnapshot.getValue(String.class);
if(provider.equals("google.com")) {
Uri photoUrl = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
String originalPieceOfUrl = "s96-c/photo.jpg";
String newPieceOfUrlToAdd = "s400-c/photo.jpg";
String photoPath = photoUrl.toString();
String newString = photoPath.replace(originalPieceOfUrl, newPieceOfUrlToAdd);
profileImage = newString;
} else if(provider.equals("facebook.com")) {
profileImage = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl().toString() + "?type=large";
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
FirebaseDatabase.getInstance().getReference().child("debates").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
if(postSnapshot.getKey().equals(key)) {
discussion = postSnapshot.getValue(String.class);
break;
}
break;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
MessagesObject objDisc = new MessagesObject(username, discussion, profileImage);
resultsMessages.add(objDisc);
mMessagesAdapter.notifyDataSetChanged();
} else if(opposite.equals("disagree") && choice.equals("agree")) {
DatabaseReference found = FirebaseDatabase.getInstance().getReference().child("users").child(postSnapshot.getKey());
found.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
if(postSnapshot.getKey().equals("username")) {
username = postSnapshot.getValue(String.class);
}
if(postSnapshot.getKey().equals("providerId")) {
String provider = postSnapshot.getValue(String.class);
if(provider.equals("google.com")) {
Uri photoUrl = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
String originalPieceOfUrl = "s96-c/photo.jpg";
String newPieceOfUrlToAdd = "s400-c/photo.jpg";
String photoPath = photoUrl.toString();
String newString = photoPath.replace(originalPieceOfUrl, newPieceOfUrlToAdd);
profileImage = newString;
} else if(provider.equals("facebook.com")) {
profileImage = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl().toString() + "?type=large";
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
FirebaseDatabase.getInstance().getReference().child("debates").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
if(postSnapshot.getKey().equals(key)) {
discussion = postSnapshot.getValue(String.class);
break;
}
break;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
MessagesObject objDisc = new MessagesObject(username, discussion, profileImage);
resultsMessages.add(objDisc);
mMessagesAdapter.notifyDataSetChanged();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
So what's happening is I'm getting the user's username, their profile image, and figuring out the value, which I called discussion after determining the valid key. These values are all being accessed from different tree structures from the same real-time database.
Now, at the end of the if or else-if statement, I create and instantiate the MessagesObject by passing in the username, profile image, and discussion variables. I then add this object to the List<MessagesObject> called resultMessages. I then notify my custom adapter mMessagesAdapter that the data has changed.
Like I said, all the other pieces of code work perfectly fine. It's just when I pass username, discussion, and profileImage it always passes an empty string. I know this from using the debugger.
Why is that? It should not be doing that.
My problem is I cant delete a single child from the firebase database, either nothing happens or it deletes the whole child and all data. I've looked around and tried multiple solutions but for whatever reason mine wont work and I out of ideas why.
Any help is appreciated.
Java class.
//get data passed from viewpasswords and put them into textviews
name.setText(getIntent().getExtras().getString("data"));
password.setText(getIntent().getExtras().getString("pass"));
//get current user id and reference to database
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
assert user != null;
userID = user.getUid();
dbRef = FirebaseDatabase.getInstance().getReference();
//button onclick stuff
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DeleteData();
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(DecryptPassword.this, ViewPasswords.class);
startActivity(intent);
}
});
}
private void DeleteData() {
final String passName = name.getText().toString();
Query query = dbRef.child("Passwords").child(userID).orderByChild("PasswordName").equalTo(passName);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getChildrenCount() > 0){
for(DataSnapshot itemSnapshot : dataSnapshot.getChildren()){
dataSnapshot.getRef().removeValue();
Toast.makeText(DecryptPassword.this, "Success", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(DecryptPassword.this, ViewPasswords.class);
startActivity(intent);
}
}else{
Toast.makeText(DecryptPassword.this, "Failed to delete", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(DecryptPassword.this, "Error occurred sorry", Toast.LENGTH_SHORT).show();
}
});
}
Firebase Database:
Thanks.
dataSnapshot.getRef().removeValue();
Change this part to:
itemSnapshot.removeValue();
Good Day.
I have tried my level best but failed to update the data whenever i enter new "Name","Age" or anything the data entered before is wiped.I want to use push or something else to update the data.But still failing to do that.
nameRef.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
#Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
String ho = name.getText().toString();
nameRef.setValue(ho);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
genderRef.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
#Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
String ho = gender.getText().toString();
genderRef.setValue(ho);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
ageRef.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
#Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
`enter code here` String text = dataSnapshot.getValue(String.class);
String ho = age.getText().toString();
ageRef.setValue(ho);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
EmailRef.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
#Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
String ho = email.getText().toString();
EmailRef.setValue(ho);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
If you have a JSON tree like this:
->users
->uid
->name
->age
->gender
->email
Then to update the values in the database you should do something like this:
DatabaseReference mFirebaseDBRef = FirebaseDatabase.getInstance().getReference();
FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
//use a POJO and make sure to have a default empty constructor in that class definition
User user = new User(name, age, gender, email)
mFirebaseDBRef.child("users").child(mFirebaseUser.getUid()).setValue(user);
And that should update your database! If something is not clear, feel free to ask.