I am new to firebase and nosql database
I am trying to retrieve some specific data from a Firebase. I have a node universities and that node has many unique identifier as node which has so more data into it. I want to retrieve name from each node.
Please have a look at this.
What I have tried so far: I tried using addChildEventListener but this only listens first child. I've debugged it and it was only showing the values of the first child of universities node.
myRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
List<University> university = new ArrayList<>();
Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();
Log.d("TAG",dataSnapshot.getValue().toString());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I have also tried addValueEventListener. This listens the whole node and return whole data, but I am unable to extract "name" for it since it contains unique identifiers.
Please guide me into the right directions.
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
University university = dataSnapshot.getValue(University.class);
List<University> universities = (List<University>) dataSnapshot.getValue(University.class);
*//*List<String> lst = new ArrayList<String>();
for(DataSnapshot dsp : dataSnapshot.getChildren()){
lst.add(dsp.getKey());
}
Log.d("TAG",lst.toString());*//*
Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();
*//*List<Object> values = new ArrayList<Object>(td.values());
List<String> list=new ArrayList<String>();
for(int i=0; i<values.size(); i++){
list.add((String) values.get(i));
}*//*
Log.d("TAG", university.toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
If you using addValueEventListener for retrieve all university from Firebase
List<University> universityList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
universityList.clear();
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
University university = postSnapshot.getValue(University.class);
universityList.add(university);
// here you can access to name property like university.name
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
Or if you using addChildEventListener
List<University> universityList = new ArrayList<>();
myRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
University university = dataSnapshot.getValue(University.class);
universityList.add(university);
Log.i(TAG,"add university name = " + university.name);
...
}
...
}
Regarding Linh's answer about using the addEventListener method. You can use GenericTypeIndicator in order to directly create the list of objects you'll be needing. Here's a quick example:
List<University> universityList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<List<University>> t = new GenericTypeIndicator<List<University>>() {};
universityList = snapshot.getValue(t);
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
Hope this answer helps.
You can use equalTo() of Query in which you can pass your custom string to match with firebase database.
like this way you can create your Query and set listener with that
Query queryRef = myFirebaseRef.orderByChild("name").equalTo("some name");
refer this thread for more.
Here is nice example from firebase officials, refer that and you will get clear idea
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
User user = postSnapshot.getValue(User.class);
list.add(user);
}
}
class User
class User{
String name;
String phone;
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getphone() {
return phone;
}
public void setphone(String phone) {
this.phone = phone;
}
List bind with class
List<User> list== new ArrayList <>();
100% it will work
I had the same problem .You can just change the access specifier i.e public to private in the Model class you problem will be solved
Related
private Flowable<List<String>> getInfo(){
List<String> myList = new ArrayList<>();
return BehaviorProcessor.create(emitter->{
FirebaseDatabase.getInstance().getReference("url")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (null != dataSnapshot) {
if (dataSnapshot.exists()) {
if (dataSnapshot.hasChildren()) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
final MyClass myClass= postSnapshot.getValue(MyClass.class);
String key = postSnapshot.getKey();
String firsnName = myClass.getFirstName();
myList.add(0, key);
myList.add(1, firsnName);
}
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}, BackpressureStrategy.LATEST);
}
private Flowable<GeoLocation> setMyLocation(){
Flowable.create(emitter -> {
geoFire.setLocation(key, new GeoLocation(myLastLocation.getLatitude(),
myLastLocation.getLongitude()), (k, e) ->{
});
});
}
I would like to pass the values in my list into my next method. Specifically, I need to pass my key into the setLocation method of GeoFire as the key as shown. I do understand onDataChange is called asynchronously and I am used to doing it in my Activity class using a callbackListener. How can I do the above in my ViewModel? I am open to change the whole implementation for a working solution.
I am using ViewModel, LiveDataReactiveStream in Android Java. I would hope to get help finding a solution using RxJava inside my ViewModel.
Thanks.
Don't use List, create your own object, say Info :
class Info {
private final String key;
private final String firstName;
public Info(String key, String firstName) {
this.key = key;
this.firstName = firstName;
}
...
}
Your getInfo then will be like :
private Flowable<Info> getInfo(){
return BehaviorProcessor.create(emitter->{
FirebaseDatabase.getInstance().getReference("url")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (null != dataSnapshot) {
if (dataSnapshot.exists()) {
if (dataSnapshot.hasChildren()) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
final MyClass myClass= postSnapshot.getValue(MyClass.class);
String key = postSnapshot.getKey();
String firstName = myClass.getFirstName();
// use emitter here
emitter.onNext(new Info(key, firstName))
}
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
// use emitter here
emitter.onError(/*error*/)
}
});
}, BackpressureStrategy.LATEST);
}
and then change the signature of your setMyLocation to accept the key.
After that use concatMap operator to chain your requests :
getInfo()
.concatMap(info -> setMyLocation(info.key))
.subscribe();
Hope this helps
So I'm trying to display the data related to the user that has logged in but the data related to it is returning me null I've tried multiple solutions but it didn't work
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
DatabaseReference userdata;
String namedata;
public HomeViewModel() {
userdata = FirebaseDatabase.getInstance().getReference();
userdata.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snap : dataSnapshot.getChildren()){
User user = snap.getValue(User.class);
namedata = user.getUserId();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
mText = new MutableLiveData<>();
mText.setValue("Welcome back! " +namedata);
}
public LiveData<String> getText() {
return mText;
}}`
here is the User.class I want to get those data and display it in the views
public class User {
String userId;
String userPhone;
String userGender;
public User() {
}
public User(String userId, String userPhone, String userGender) {
this.userId = userId;
this.userPhone = userPhone;
this.userGender = userGender;
}
public String getUserId() {
return userId;
}
public String getUserPhone() {
return userPhone;
}
public String getUserGender() {
return userGender;
}}
You have to call the "userId"
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
HashMap<String, String> map = (HashMap<String, String>) snap.getValue();
if (map.containsKey("userId")) {
if (map.get("userId").equals("william henry")) {
String userGender = map.get("userGender");
}
}
}
}
or
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
HashMap<String, String> map = (HashMap<String, String>) snap.getValue();
if (map.containsKey("userId")) {
String userGender = map.get("userId");
}
}
}
You are getting null because you are not fetching value from correct node. There are child of users and you want to get values of child of user.
userdata = FirebaseDatabase.getInstance().getReference().child("User");
userdata.addOnChildEventListener( new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot snap, String previousChildName) {
//get value here.
User user = snap.getValue(User.class);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onCancelled(DatabaseError error) {
}
});
You want to get single user data then
userdata = FirebaseDatabase.getInstance().getReference().child("User").child(userIdOfthatuser);
userdata.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snap) {
User user = snap.getValue(User.class);
namedata = user.getUserId();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
first FirebaseDatabase.getInstance().getReference() point to your root ( which is the link above your database).so you need to point to what you want to get ( which is User tree).
Second of all,you must set your data inside onDataChange cause it not synchonize function ( mean your name data is always not follow the data return from onDataChange)
FirebaseDatabase.getInstance().getReference().child("User").orderByChild("userId").equalTo(*your_logged_in_user_id*).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Iterator iterator = dataSnapshot.getChildren().iterator()
if(iterator.hasNext()){
User user = iterator.next().getValue(User.class)
}
name = user.getName();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I am trying to get data from nested nodes under unique keys. Each key is identical. It's difficult for me to deal with such problem help please.
I have tried ChildEventListener on database reference but not succeeded.
here is the code i am using to retreive data
InfoFragment.java
auth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("Seller").getRef().child("ImpInfo");
mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postDataSnapshot : dataSnapshot.getChildren()) {
ShopSellerInfo shopSellerInfo = postDataSnapshot.getValue(ShopSellerInfo.class);
mShopSellerInfo.add(shopSellerInfo);
}
mNearBySellerAdapter = new NearBySellerAdapter(getContext(), mShopSellerInfo);
mRecyclerView.setAdapter(mNearBySellerAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
return rootView;`
ShopSellerInfo.java
public ShopSellerInfo(String shopAddress, String shopPhoneNo, String
shopImageUrl, String shopName) {
this.shopAddress = shopAddress;
this.shopPhoneNo = shopPhoneNo;
this.shopImageUrl = shopImageUrl;
this.shopName = shopName;
}
public ShopSellerInfo() {
}
public String getShopAddress() {
return shopAddress;
}
public String getShopPhoneNo() {
return shopPhoneNo;
}
public String getShopImageUrl() {
return shopImageUrl;
}
public String getShopName() {
return shopName;
}
public void setUserAddress(String shopAddress) {
this.shopAddress = shopAddress;
}
public void setUserPhoneNo(String shopPhoneNo) {
this.shopPhoneNo = shopPhoneNo;
}
public void setImageUrl(String shopImageUrl) {
this.shopImageUrl = shopImageUrl;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
}
This is the structure of my Database
I have a specific node in each unique key the contain data. I want to retrieve that data form every child node and show on single activity.
You're not too far off, just a few mistakes. The following is closer to what you need:
mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("Seller");
mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot sellerSnapshot : dataSnapshot.getChildren()) {
DataSnapshot impInfoSnapshot = sellerSnapshot.child("ImpInfo");
ShopSellerInfo shopSellerInfo = impInfoSnapshot.getValue(ShopSellerInfo.class);
mShopSellerInfo.add(shopSellerInfo);
mNearBySellerAdapter = new NearBySellerAdapter(getContext(), mShopSellerInfo);
mRecyclerView.setAdapter(mNearBySellerAdapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
Changes:
Removed the call to .getRef(), which is not needed and in general an anti-pattern.
Removed the call to .child("ImpInfo") from the DatabaseReference, since there is no /Seller/ImpInfo.
Added .child("ImpInfo") in the loop, since you want the ImpInfo child of each snapshot.
Raise an error if onCancelled triggers, since it's a bad practice to ignore errors.
The structure looks like this:
users
|----username1
|----uid:value
username2
|----uid:value
I'm trying to find the best way to get the username value based of the value of it's uid,
This need to be in Java code (Android), So far I found the following code:
Query uid = reference.child("users").orderByChild("uid").equalTo(uid);
uid.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
if (keys.equals(uid)) {
// uid found
} else {
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try this code and create a model class to fetch value of Uid
DatabaseReference users= FirebaseDatabase.getInstance().getReference().child("users");
users.orderByChild("uid").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot post:dataSnapshot.getChildren()){
//Use a model class to fetch user id like below
UIDDetails u=post.getValue(UIDDetails.class);
String user_id=u.getUid();
//Here all values will be equal to youy required uid if exists more than one
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Please add below code into your file:
DatabaseReference users= FirebaseDatabase.getInstance().getReference().child("users");
final Query userQuery = users.orderByChild("uid");
userQuery.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
map.clear();
if(child.getkey().toString().equalsIgnoreCase(uid)){
//Get the node from the datasnapshot
String myParentNode = dataSnapshot.getKey();
for (DataSnapshot child: dataSnapshot.getChildren())
{
String key = child.getKey().toString();
String value = child.getValue().toString();
map.put(key,value);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
databaseError.toException();
}
});
How do I return combinedFoodItem from the code below. I have been using firebase for less than a week now and I still find this confusing. The summary is:
Food contains name and categoryId (which is from the FoodOption model)
public void getFoodItems() {
//get all the food options for the selected venue
final List<FoodItem> combinedFoodItem = new ArrayList<>();
filterRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
final FoodOption foodOption = dataSnapshot1.getValue(FoodOption.class);
final List<Food> foodList = new ArrayList<>();
meniItemFilter = menuItemReference.orderByChild("categoryId").equalTo(foodOption.getKey());
meniItemFilter.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot2 : dataSnapshot.getChildren()) {
Food food = dataSnapshot2.getValue(Food.class);
foodList.add(food);
}
FoodItem foodItem = new FoodItem(foodOption.getFoodCategory(), foodList, R.drawable.ic_banjo);
combinedFoodItem.add(foodItem);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
as firebase works in a reactive manner you can't return elements but listen for changes of them. You can define your combinedFoodItem outside the function (this is, for instance, as a class field) and then add the elements to the list (or clear the list every time the data changes if you want to reset the list this depends on the behavior of your list)