I want to retrieve only the date and all the date element at once and store them in array in the firebase as shown below.
i have used the following code to retrieve but failed to that.
public void showData(View view) {
final ArrayList<String> date = new ArrayList<>();
firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Date");
firebaseDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
date.add(dataSnapshot.getValue().toString());
display.setText(dataSnapshot.getValue().toString());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Please help to find a solution for this.
The firebase reference is wrong, you should go from the top node to the date node.
Change this:
firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Date");
into this:
firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Name");
firebaseDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
date.add(datas.child("Date").getValue().toString());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Since you want to get all Date then put the firebase reference at the node Name and loop inside the names to be able to retrieve all the dates.
If you want to get the date of one user example Ravi, then you do not need to loop, you can do the following:
firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Name").child("Ravi").child("Date");
firebaseDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
date.add(dataSnapshot.getValue().toString());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
As said by #PeterHaddad in the previous answer, the sequence to access the data you have taken is not right. You may try this way, if nothing else helped until yet.
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("Names").orderByChild("Date").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
date.add(ds.getValue(String.class));
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
//Also as you've told that code is not working, please tell, what exactly is not working
Related
ccontacts = new ArrayList<>();
DatabaseReference dbGelenler = db.getReference("Filmler");
dbGelenler.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot gelenler:dataSnapshot.getChildren()){
ccontacts.add(new Data(gelenler.getValue(Film.class).getAciklama(),gelenler.getValue(Film.class).getBen(),gelenler.getValue(Film.class).getImdb(),gelenler.getValue(Film.class).getIsim(),gelenler.getValue(Film.class).getIzlemeLinki(),gelenler.getValue(Film.class).getKategori(),gelenler.getValue(Film.class).getNe(),gelenler.getValue(Film.class).getService(),gelenler.getValue(Film.class).getTrailer(),gelenler.getValue(Film.class).getUrl(),gelenler.getValue(Film.class).getWhat(),gelenler.getValue(Film.class).getYear()));
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
for (Data d:ccontacts){
b.setText(d.getIsim());
}
When I directly set the text via gelenler.getValue() it works, however when I want to populate an ArrayList with this information it fails and receives null. Any help would be appreciated.
I need to get the date and timing data by using the userid (DcEUNNJSB20WhmyL1IsJsk7YnQ1). Can someone guide me on this problem? Thanks in advance.
databaseReference= FirebaseDatabase.getInstance().getReference("appointment");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String userid1 = FirebaseAuth.getInstance().getCurrentUser().getUid();
for(DataSnapshot dataSnapshot1: dataSnapshot.child("appointment").getChildren()){
if(dataSnapshot1.getValue(AppointmentObject.class).getUserid().equals(userid1)){
AppointmentObject thera= dataSnapshot1.getValue(AppointmentObject.class);
a.add(thera);
}
}
adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a);
rv.setAdapter(adapter);
}
You're looking for a query. In this case, since you're trying to order/filter on a child property, you'll want to use orderByChild(...). Something like:
databaseReference = FirebaseDatabase.getInstance().getReference("appointment").child("qNTrPz0B5hVlafqzRueEbbiUqj1");
Query query = databaseReference.orderByChild("userid").equalTo("DcEUNNJSB20WhmyL1IsJsk7YnQ1");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()){
System.out.println(childSnapshot.getKey());
System.out.println(childSnapshot.child("date").getValue(String.class));
}
}
public void onCancelled(#NonNull DatabaseError databaseError) { throw databaseError.toException(); }
}
This question already has answers here:
How to return DataSnapshot value as a result of a method?
(6 answers)
Closed 4 years ago.
before reading I'm a newbie trying to learn android development so don't go hard on me, please.
I have this Realtime database firebase :
I'm trying to check if the entered email exists , i've tried a method but it did not work out for me.
I tried this method :
private boolean checkIfExistes() {
reference.child("Users").orderByChild("email").equalTo(Email.getText().toString()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
CheckExists =true;
else
CheckExists =false;
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
if (CheckExists)
return true;
else
return false;
}
it always returns false though even if the email exists. Help please.
Try this:
boolean CheckExists =false; //declare and assign default value in global scope
reference.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> userChildren = dataSnapshot.getChildren();
for (DataSnapshot user: userChildren) {
User u = user.getValue(User.class); //make a model User with necessary fields
if(u.email.equalsIgnoreCase(Email.getText().toString())){
CheckExists =true;
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
private DatabaseReference rootRef,userRef;
get the initialize firebase in oncreare method
rootRef = FirebaseDatabase.getInstance().getReference();
userRef = rootRef.child("Users");
here the code for checking email exist or not
userRef.orderByChild("email").equalTo(emailEdt.getText().toString())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
hideProgress();
if (dataSnapshot.exists()){
Toast.makeText(LoginActivity.this,"User already exists",Toast.LENGTH_SHORT).show();
}else {
UserLogin user = new UserLogin(emailEdt.getText().toString(),profilePath);
DatabaseReference db = userRef.push();
db.setValue(user);
//Log.d(TAG,"user key is::: "+db.getKey());
prefs.savEmail(emailEdt.getText().toString());
prefs.savProfileUrl(profilePath);
prefs.savKey(db.getKey());
openMainActivity();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
hideProgress();
Toast.makeText(LoginActivity.this,databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});
My firebase database looks like this:
From this query
String currentUserID = FirebaseAuth
.getInstace()
.getCurrentUser()
.getUid();
DatabaseReference favorsRef = FirebaseDatabase
.getInstance()
.getReference()
.child("favors");
Query myChatsQuery = favorsRef
.orderByChild("uid")
.equalTo(currentUserID);
I need to create other query to get just the tuples from myChatsQuery who has the child called "messages". If the tuple doesn't have the child "messages", I don't want to insert it in my new query.
So, How I can create a query from other query?
Help me please,
Thanks in advance.
Unfortunately, orderByChild() does not work on multiple same fields at once, so you may have to do the job in two steps.
First you can run an orderByChild() query looking for the currentUserId and then add all those that match to an ArrayList and then run orderByChild() in those found uids for messages.
This looks something like this, in code:
reference.orderByChild("uid").equalTo(currentUserId).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
array.add(dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
)};
This code above adds uids matching currentUserId to the ArrayList array.
databaseReference.child(array.get(0)).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.child("messages").exists())
// do your thing
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
)};
You can also just use a for loop to go through all the values in the array.
To solve this, please use the following code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("favors").orderByChild("uid").equalTo(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
if(ds.child("messages").exists()) {
//Do what you need to do
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
query.addListenerForSingleValueEvent(valueEventListener);
As you can see, you can solve this by querying the database only once.
Firebase just silently don't get the data.
I work on it a lot of time but still don't really understand what is a problem.
Probably it's very simple mistake so get my excuses please
fRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ids = dataSnapshot.child("dictionary").child("fruitIds").getValue(DictionaryIDs.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
DictionaryIds.class code
package com.example.asuper.mluvitslova;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
public class DictionaryIDs {
private ArrayList<String> fruitIds = new ArrayList<>();
public DictionaryIDs() {
}
public DictionaryIDs(ArrayList<String> fruitIds) {
this.fruitIds = fruitIds;
}
public void addFruitId(String id){
fruitIds.add(id);
}
public ArrayList<String> getFruitIds(){
return fruitIds;
}
public void setFruitIds(ArrayList<String> fruitIds){
this.fruitIds = fruitIds;
}
}
And in log just NOTHING
This code must just get me the DictionaryIds.class file but in fact Firebase just get me null object.
Firebase database structure
Under the last fruitIds child, there are no DictionaryIDs objects, there are simple strings. To get all those ids, you need to loop through the DataSnapshot object using getChildren() method like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference fruitRef = rootRef.child("dictionary").child("fruitIds").child("fruitIds");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String id = ds.getValue(String.class);
Log.d(TAG, id);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
fruitRef.addListenerForSingleValueEvent(valueEventListener);
The result in your logcat will be all those ids.