Model Class returns null - java

[model class is returning null, data is coming from Firebase database but upon getting imageUrl its giving null.
Actually, I am trying to get images urls from database which are previously saved.
firebaseDatabase=FirebaseDatabase.getInstance();
databaseReference=firebaseDatabase.getInstance().
getReference("Catagories");
mupload=new ArrayList<>();`
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot post : dataSnapshot.getChildren())
{
Upload upload=post.getValue(Upload.class);
mupload.add(upload);
}
customQuoteAdapter=new CustomQuoteAdapter( Images.this,
mupload);
recyclerView.setAdapter(customQuoteAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),
"ERROR"+databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});

You have a bussiness (sic) level in your JSON, that you're not handling in your code. The simplest way to fix this is to attach your listener to that bussiness node:
databaseReference=firebaseDatabase.getInstance().getReference("Catagories/Bussiness");
mupload=new ArrayList<>();`
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot post : dataSnapshot.getChildren()) {
Upload upload=post.getValue(Upload.class);
mupload.add(upload);
}
customQuoteAdapter=new CustomQuoteAdapter( Images.this, mupload);
recyclerView.setAdapter(customQuoteAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "ERROR"+databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
If you want to get all URLS for all nodes under Categories, you'll need to add a loop inside onDataChange. Something like:
databaseReference=firebaseDatabase.getInstance().getReference("Catagories");
mupload=new ArrayList<>();`
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot categorySnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot linkSnapshot : categorySnapshot.getChildren()) {
Upload upload=linkSnapshot.getValue(Upload.class);
mupload.add(upload);
}
}
customQuoteAdapter=new CustomQuoteAdapter( Images.this, mupload);
recyclerView.setAdapter(customQuoteAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "ERROR"+databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});

Should it be?
public String getImageUrl() {
return this.imageUrl;
}

Related

Show list of users with comparison two references in Android Firebase

In Firebase I have the "users" where I save all users' information.
Now I want to show someone I like in the "Favor".
This is "Favors"
In "Favor", only my ID and people ID I added to the favor list
And this is "users":
In "users", I have all users' information inside people's ID
And this is my code to show all users
database.getReference().child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
users.clear();
for(DataSnapshot snapshot1: snapshot.getChildren()){
User user= snapshot1.getValue(User.class);
if(!user.getUid().equals(FirebaseAuth.getInstance().getUid()) && hasChat != null)
users.add(user);
}
binding.recyclerView.hideShimmerAdapter();
usersAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
And this code i'm trying to show favor list, but it's show blank:
database.getReference().child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
users.clear();
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
User user = snapshot1.getValue(User.class);
if (!user.getUid().equals(FirebaseAuth.getInstance().getUid()) && user.getUid().equals(database.getReference().child("Favors").getKey()))
users.add(user);
}
binding.recyclerView.hideShimmerAdapter();
usersAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});

Firebase Database Not in Sync

I want to keep the addToCart button in sync with the Firebase database. The button should be enabled if the checkForActiveOrdersQuery has no children, otherwise, it should be disabled.
The issue with the below code is that the keepSynced(true) is not working.
checkForActiveOrders = FirebaseDatabase.getInstance().getReference("activeOrders");
checkForActiveOrdersQuery = checkForActiveOrders.child(restaurantID).child(tableID);
checkForActiveOrdersQuery.keepSynced(true);
checkForActiveOrdersQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String orderIDKey = dataSnapshot1.getKey();
if (!dataSnapshot.hasChildren()) {
addToCartButton.setEnabled(true);
} else if (!orderIDKey.equals(orderID)){
addToCartButton.setEnabled(false);
} else if (orderIDKey.equals(orderID)) {
addToCartButton.setEnabled(true);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

How to get the last data from realtime database of firebase in Android

I need to get the data which is the "2019-05-15". NOT the inside of the "2019-05-15". Also this is NOT the current date today.
"data"
{
"2019-05-03": {
"Name": "asdasdasd",
"Phone": "0934753423423"
},
"2019-05-15": {
"Name": "zxczxc",
"Phone": "8745837456038"
}
}
I tried the current date but im getting error of it. I just need the "2019-05-15" to get
myRef=FirebaseDatabase.getInstance().getReference().child("data");
Query query = myRef.orderByKey().limitToLast(1);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Log.d("Date", dataSnapshot.child("").getValue().toString());
for (DataSnapshot child: dataSnapshot.getChildren()) {
Log.d("User val", child.child("Name").getValue().toString());
Log.d("Date", dataSnapshot.child("").getValue().toString()); // What should I write here?
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Thanks in advance
Try the following:
myRef=FirebaseDatabase.getInstance().getReference().child("data");
Query query = myRef.orderByKey().limitToLast(1);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
String key = child.getKey();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
First, add a reference to the parent node data, then using getKey() you will be able to retrieve the date.

how to get string array from firebase realtime database

databaseReference = FirebaseDatabase.getInstance().getReference("/sample");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: dataSnapshot "+dataSnapshot.getValue());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I'm new to android app development and firebase as well. i m fetching data from sample node and getting DataSnapshot value like below.
{size=[Small, Large, Ex-Large], type=[Type 1, Type 2], color=[Red, Green, Blue], category=[T-Shirt, Jeans, Sweater]}
need some expect help, any suggestion will greatly appreciated.
Thanks
To retrieve values separately, you can use a code like this:
databaseReference.child("category").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (int i=0;i<3;i++) {
// category is an ArrayList you can declare above
category.add(dataSnapshot.child(String.valueOf(i)).getValue(String.class));
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { // Do something for this
}
});
Similarly you can add values of other nodes in your other ArrayLists, just by changing the value of Childs in this code.
Firebase has no native support for arrays. If you store an array, it really gets stored as an "object" with integers as the key names.
// we send this
['hello', 'world']
// Firebase stores this
{0: 'hello', 1: 'world'}
Best Practices: Arrays in Firebase
// TRY THIS
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
youNameArray = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String data = snapshot.getValue(String.class);
youNameArray.add(data);
}
Log.v("asdf", "First data : " + youNameArray.get(0));
}
Something like this:
databaseReference = FirebaseDatabase.getInstance().getReference("/sample");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot sampleSnapshot: dataSnapshot.getChildren()) {
Log.d(TAG, "onDataChange: sampleSnapshot "+sampleSnapshot.getKey()+" = "+sampleSnapshot.getValue());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
The difference is that in my answer I loop over dataSnapshot.getChildren() to get each individual sample snapshot. The sampleSnapshot.getValue() call should return a List.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference refence = database.getReference();
refence.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot snapshot) {
// TODO Auto-generated method stub
ArrayList array= new ArrayList<>();
for (DataSnapshot ds : snapshot.getChildren()){
String data = ds.getValue().toString();
array.add(data);
}
System.out.println(array);
}
#Override
public void onCancelled(DatabaseError error) {
// TODO Auto-generated method stub
}
});
In my case String.class does not work instead .toString method works
String data = ds.getValue().toString();

What is the best way to check the existence of every child in one node?

FirebaseDatabase.getInstance().getReference("Block").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
if(snap.getKey().equals(myUID)){
FirebaseDatabase.getInstance().getReference("Block").child(myUID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
if(snap.getKey().equals(list.get(position).getUid())){
FirebaseDatabase.getInstance().getReference("Block").child(myUID).child(list.get(position).getUid()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
if(snap.getValue().equals("1")){
call_method();
}else{
//code...
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
call_method();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
call_method();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
This code works well, but it is very long so I am asking for the best way to write it ?
I want to check firstly if Block node has child equals to my UID if it is true then I want to check if my UID has child equals to specific UID if it is true then I want to get the value of block which is 0 in this example
How can I do that ? Thank you very much.
Just traverse to the path specified using child. You will get no data if the path doesn't exist.
FirebaseDatabase.getInstance().getReference("Block")
.child("MyUUID")
.child("specificUUID")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
// ... Do something
} else {
call_method();
}
// ... Other code
}
Try this:
Firebaseuser user=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Block");
ref.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exist()){
//checks first uid
if(dataSnapshot.hasChild(seconduid){
//checks if seconduid is there
for(DataSnapshot datas:dataSnapshot.getChildren()){
String blocks=datas.child("block").getValue().toString();
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
This will check the first uid if(dataSnapshot.exist()) after that it will check if that uid node has the second uid that you provide if(dataSnapshot.hasChild(seconduid){ then you will iterate and get the node block

Categories