Android Retriving the data from firebase using Phone Number - java

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

Related

Unable to retrieve and compare data from firebase in android studio

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) {
}
});`

How to retrieve the push id value

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

Get data from firebase from a specific position

Hy, I'm writing an application that has to get specific data from firebase using the position of the item in the listView. My problem is that I have no idea how to take it this item on firebase.
For all child of Torneo I have to control all the nameCreator.
I have tried this:
public Boolean RegisterUser(Data data, final int position, final Context c){
boolean registration;
final ArrayList<String> Creator = new ArrayList<>();
databaseReference.orderByChild("Tornei").equalTo(Integer.toString(position)).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
Creator.add(data.child("nameCreator").getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
if(Creator.equals(data.getNameCreator())){
registration = false;
}else{
registration = true;
}
return registration;
}
Data is a class with some getter and setter that I have created.
position is the position of the element on the list view.
Thanks for answers.
Change the following:
databaseReference.orderByChild("Tornei").equalTo(Integer.toString(position)).addListenerForSingleValueEvent(new ValueEventListener() {
into this:
databaseReference.child("Tornei").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
Creator.add(datas.child("nameCreator").getValue().toString());
if(Creator.equals(data.getNameCreator())){
registration = false;
}else{
registration = true;
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Then you will be able to loop and retrieve the value of nameCreator
It's easy.
【Step 1 | Get Snapshot Data and Save in Global Variable】
DatabaseReference rootReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference fruitsReference = rootReference.child("fruits");
DataSnapshot fruitsData;
#Override
protected void onStart() {
super.onStart();
fruitsReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshots) {
fruitsData = dataSnapshots;
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
【Step 2 | Find Your Target Position through the Loop】
public void onClick(View view) {
int index = 0;
for (DataSnapshot childSnapshot : fruitsData.getChildren()) {
if (index == 1) { //your target position
DatabaseReference currentFruitReference = childSnapshot.getRef();
currentFruitReference.setValue("peach"); //do whatever you want
}
index++;
}
}

Android firebase - show specific data

I'm trying to create a Firebase Realtime database project in which the data stored is either type A or type B, etc. This is how it looks like on the console:
Firebase Database
The user can then add more posts, of which are saved to the database then displayed on a ListView.
My Code
final DatabaseReference room = FirebaseDatabase.getInstance().getReference().getRoot();
chatRoomAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, chatList);
lvChatRoom.setAdapter(chatRoomAdapter);
btnAddChatRoomMa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strChatRoomName = etChatRoom.getText().toString();
etChatRoom.setText("");
Map<String, Object> chatMap = new HashMap<>();
chatMap.put(strChatRoomName + " -TYPEA", "");
room.updateChildren(chatMap);
}
});
room.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator iterator = dataSnapshot.getChildren().iterator();
Set<String> set = new HashSet<>();
while (iterator.hasNext()) {
set.add(((DataSnapshot) iterator.next()).getKey());
}
chatList.clear();
chatList.addAll(set);
chatRoomAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I only want to show for instance, TYPEA (on the database), without displaying the rest. How can I achieve this?
If you want to display only the value of the property TYPEA, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("fruit-TYPEA");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String fruitTYPEA = dataSnapshot.getValue(String.class);
Log.d("TAG", fruitTYPEA);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);
The output will be: orange.

Firebase Delete Child onButtonClick

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

Categories