Error while updating to Firebase Database - java

I'm having a problem while trying to update to Firebase real time database.
I'm getting the error "com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
I think this might mean I need to use another way to update the data since I'm using a recyclerView instead of a listView, but since now I have:
public void update_button(){
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
snapshot.getRef().child("title").setValue(postTitleEdit.getText());
snapshot.getRef().child("desc").setValue(postTextEdit.getText());
Toast.makeText(CowDet.this, "updated", Toast.LENGTH_LONG).show();
CowDet.this.finish();
}
The way I'm using to add data to firebase is:
mDatabaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
Blog blog = snapshot.getValue(Blog.class);
blogList.add(blog);
blogRecyclerAdapter = new BlogRecyclerAdapter(PostListActivity.this, blogList);
recyclerView.setAdapter(blogRecyclerAdapter);
blogRecyclerAdapter.notifyDataSetChanged();
}
Could someone help me update the data? I'm kinda stuck here.
Here is my Blog class:
public class Blog {
public String title;
public String desc;
public String image;
public String timestamp;
public String userid;
public Blog() {
}
public Blog(String title, String desc, String image, String timestamp, String userid, String milkQty, String note) {
this.title = title;
this.desc = desc;
this.image = image;
this.timestamp = timestamp;
this.userid = userid;
}
public Blog(String title, String desc) {
this.title = title;
this.desc = desc;
this.image = image;
this.timestamp = timestamp;
this.userid = userid;
}
public Blog(String toString) {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}
new update_button
public void update_button(){
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
databaseReference.child("Mblog").child("title").setValue(postTitleEdit);
Toast.makeText(CowDet.this, "updated", Toast.LENGTH_LONG).show();
CowDet.this.finish();
}
new error: com.google.firebase.database.DatabaseException: Found conflicting getters for name: getText
I'm getting this error on the line databaseReference.child("Mblog").child("title").setValue(postTitleEdit);

The error message is pretty explicit. Your Blog class seems to contain an array (with []), which isn't supported by Firebase's serializer. You should instead use a List class for the same purpose.
If you're having a hard time making that work, edit your question to include the Blog class.

Related

ModelCategory model = dataSnapshot.getValue(ModelCategory.class); App get Crush

App Get Crushed....
error not showing in android studio but while I run the app it get crush and marked the error in
ModelCategory model = dataSnapshot.getValue(ModelCategory.class);
All code is correct but while I the app it get crushed.... the model name is same as in firebase storage but get crush.....
private void loadCategories() {
categoryArrayList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Categories");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
categoryArrayList.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
ModelCategory model = dataSnapshot.getValue(ModelCategory.class);
categoryArrayList.add(model);
}
adapterCategory = new AdapterCategory(AdminDashboardActivity.this, categoryArrayList);
binding.categoriesRv.setAdapter(adapterCategory);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
Model Category Class: all code is correct but app get crush......
public class ModelCategory {
String id, category, uid;
long timestamp;
public ModelCategory(String id, String category, String uid, long timestamp) {
this.id = id;
this.category = category;
this.uid = uid;
this.timestamp = timestamp;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
this is the logcat error is between stars linge
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at **com.mohmand.library.AdminDashboardActivity$3.onDataChange(AdminDashboardActivity.java:78)**
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
Please add an empty constructor.
Like this.
public class ModelCategory {
String id, category, uid;
long timestamp;
public ModelCategory(String id, String category, String uid, long timestamp) {
this.id = id;
this.category = category;
this.uid = uid;
this.timestamp = timestamp;
}
public ModelCategory() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}

Can't convert object of type String to type Bitmap

Well, because none answered me I am asking again hopping that there is some polite people... I am trying to retrieve images that are uploaded on Firebase. In datasnapshot it gives the following error: Can't convert object of type java.lang.String to type android.graphics.Bitmap. I tried some ways to solve the issue but nothing changed. Here is the code:
This is the code to read the post and show it on the screen.
private void ReadPosts() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
postsList.clear();
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
Posts post = snapshot1.getValue(Posts.class);
for (String id : followingList) {
if (post.getPublisher().equals(id)) {
postsList.add(post);
}
}
}
postsAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
This is the model code.
public class Posts {
private Bitmap postImage;
private String postText;
private String publisher;
private String postId;
public Posts(Bitmap postImage, String postText, String publisher, String postId) {
this.postImage = postImage;
this.postText = postText;
this.publisher = publisher;
this.postId = postId;
}
public Posts() {
}
public String getPostId() {
return postId;
}
public void setPostId(String postId) {
this.postId = postId;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Bitmap getPostImage() {
return postImage;
}
public void setPostImage(Bitmap postImage) {
this.postImage = postImage;
}
public String getPostText() {
return postText;
}
public void setPostText(String postText) {
this.postText = postText;
}
}
Please this time help me. It is meaningless to be on a forum and none helping..
The problem is that the snapshot from the database returning postImage with a String (probably the link of the image) and in your model class, postImage is declared with a Bitmap. Hence, the String cannot be converted to Bitmap directly. So, you have to change the model class like:
from private Bitmap postImage; to private String postImage;
Then you have to load the image from the link (may be using Glide):
If the link is from FirebaseStorage you can load the image like this:
StorageReference ref = storageReference.child(posts.postImage).getDownloadUrl();
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(ref)
.into(imageView);
If the link is from direct link of image, you can do like this:
Glide.with(this /* context */)
.load(posts.postImage)
.into(imageView);
Note: you will need gradle dependency of Glide:
implementation 'com.github.bumptech.glide:glide:4.12.0'

Firebase returning a null dataSnapshot when loading into nested objects

I'm trying to load an object from my database but the dataSnapshot is not returning anything. I have a structure like this:
This is my Event object:
public class Event implements Serializable {
private String eventName, eventDescription;
private Author author;
private Venue venue;
private ArrayList<Comment> comments;
private ArrayList<Like> likes;
public Event() {
}
public Event(#NonNull String eventName, String eventDescription, Author author, Venue venue) {
this.eventName = eventName;
this.eventDescription = eventDescription;
this.author = author;
this.venue = venue;
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public Venue getVenue() {
return venue;
}
public void setVenue(Venue venue) {
this.venue = venue;
}
public String getEventDescription() {
return eventDescription;
}
public void setEventDescription(String eventDescription) {
this.eventDescription = eventDescription;
}
public List<Comment> getComments() {
return comments;
}
public void setComments(ArrayList<Comment> comments) {
this.comments = comments;
}
public List<Like> getLikes() {
return likes;
}
public void setLikes(ArrayList<Like> likes) {
this.likes = likes;
}
}
And this is my code:
Event event;
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child("Event").child("0").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
event = dataSnapshot.getValue(Event.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The event object comes back as null even though I'm pretty sure the path is correct

Check if no documents are returned against a Firebase ui query class

I am using Firebase-UI with firestore, Now there are chances that no documents are returned against a particular query, so how would I know that?
progressBar.setVisibility(View.VISIBLE);
SharedPreferences prefs = getActivity().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String city = prefs.getString("selectedCity", "pakistan");
Toast.makeText(getActivity(), city, Toast.LENGTH_SHORT).show();
Query query = db.collection("cities/"+city+"/"+category);
FirestoreRecyclerOptions<ExploreModel> options = new FirestoreRecyclerOptions.Builder<ExploreModel>()
.setQuery(query, ExploreModel.class)
.build();
adapter = new ExploreAdapter(options);
recyclerView.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
adapter.setOnItemClickListener(new ExploreAdapter.OnItemClickListener() {
#Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
// ExploreModel exploreModel = documentSnapshot.toObject(ExploreModel.class);
// String id = documentSnapshot.getId();
String path = documentSnapshot.getReference().getPath();
Toast.makeText(getActivity(), "Position" + position + " path :" + path, Toast.LENGTH_SHORT).show();
Intent detailActivityIntent = new Intent(getActivity(), SingleItemDetailsActivity.class);
detailActivityIntent.putExtra("document_path", path);
detailActivityIntent.putExtra("category",category);
startActivity(detailActivityIntent);
}
});
There is an empty screen if no results/ documents are returned, so I want to show a proper empty screen etc.
You arent't getting anything because the name of your fieds within the model class do not match the name of the fields in your database. See mDescription vs description? Both must be the same.
To solve this, you either change the model class to look like this:
public class ExploreModel {
private String title, description, imageUrl, location;
public ExploreModel() {}
public ExploreModel(String title, String description, String imageUrl, String location) {
this.title = title;
this.description = description;
this.imageUrl = imageUrl;
this.location = location;
}
public String getTitle() { return title; }
public String getDescription() { return description; }
public String getImageUrl() { return imageUrl; }
public String getLocation() { return location; }
}
Or you can use annotations:
public class ExploreModel {
private String title, description, imageUrl, location;
public ExploreModel() {}
public ExploreModel(String title, String description, String imageUrl, String location) {
this.title = title;
this.description = description;
this.imageUrl = imageUrl;
this.location = location;
}
#PropertyName("mTitle")
public String getTitle() { return title; }
#PropertyName("mDescription")
public String getDescription() { return description; }
#PropertyName("mImageUrl")
public String getImageUrl() { return imageUrl; }
#PropertyName("mLocation")
public String getLocation() { return location; }
}

Android Firebase Realtime database error when getting a nodes children

I have a pretty weird problem. This code was working a few days ago however now it is crashing my application. I am trying to loop through a nodes children.
private void getInfo()
{
FirebaseDatabase.getInstance().getReference()
.child("ForumResponses").child(forumID).addListenerForSingleValueEvent(new ValueEventListener()
{
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
for(DataSnapshot snap : dataSnapshot.getChildren())
{
ForumResponses forumResponses = snap.getValue(ForumResponses.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError)
{
}
});
}
If i remove the below line of code the application does not crash. Why is this?
ForumResponses forumResponses = snap.getValue(ForumResponses.class);
Below is error log
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to int
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertInteger(com.google.firebase:firebase-database##16.1.0:351)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(com.google.firebase:firebase-database##16.1.0:272)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database##16.1.0:197)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database##16.1.0:178)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database##16.1.0:47)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database##16.1.0:580)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database##16.1.0:550)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database##16.1.0:420)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database##16.1.0:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database##16.1.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database##16.1.0:212)
at Activities.ViewForumResponseActivity$1.onDataChange(ViewForumResponseActivity.java:95)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database##16.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database##16.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database##16.1.0:55)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
Below is Forum Response Class
public class ForumResponses
{
private String id;
private String response;
private String author;
private String authorID;
private String usersWhoLiked;
private int liked;
public ForumResponses()
{
/*
Default no arg constructor
*/
}
public ForumResponses(String id, String response, String author, String authorID, String usersWhoLiked, int liked)
{
this.id = id;
this.response = response;
this.author = author;
this.authorID = authorID;
this.usersWhoLiked = usersWhoLiked;
this.liked = liked;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getResponse()
{
return response;
}
public void setResponse(String response)
{
this.response = response;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getAuthorID()
{
return authorID;
}
public void setAuthorID(String authorID)
{
this.authorID = authorID;
}
public String getUsersWhoLiked()
{
return usersWhoLiked;
}
public void setUsersWhoLiked(String usersWhoLiked)
{
this.usersWhoLiked = usersWhoLiked;
}
public int getLiked()
{
return liked;
}
public void setLiked(int liked)
{
this.liked = liked;
}
}
Below is image of database
You are getting the following error:
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to int
Because the type of your liked field inside your ForumResponses class is int and in your database as the field is actual an object. To solve this, either convert the field in the database to int or change the type of the field in your ForumResponses class to a Map<String, Object>.

Categories

Resources