Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save array of data in Firebase database from Android?

I am new to Firebase Database, I just want to Save the array of data in Firebase database. when I am trying to save it shows error. Below I mentioned the POJO Class and Logcat Error while save the data.

POJO Class Coding

@IgnoreExtraProperties
public class StudentReviews {

public String student_name;
public String student_class;
public String student_image;
public String student_section;

public ArrayList<Reviews> reviewsList = new ArrayList<>();

public void StudentReviews() {}

public class Reviews {
        public String subject;
        public String marks_taken;
        public String total_marks;
        public String teacher;
    }
}

Saving Data

StudentReviews studentReviews = new StudentReviews();
studentReviews.student_name = et_studentName.getText().toString();
studentReviews.student_class = et_student_class.getText().toString();
studentReviews.student_image = "";
studentReviews.student_section = sp_section.getSelectedItem().toString();
studentReviews.reviewsList = reviewsArray;

dbUploadReview.push().setValue(studentReviews); //165th Line in Logcat Error

Logcat Error

com.google.firebase.database.DatabaseException: Invalid key: this$0. Keys must not contain '/', '.', '#', '$', '[', or ']'
at com.google.android.gms.internal.zzbtf.zzjq(Unknown Source)   
at com.google.android.gms.internal.zzbtf.zzay(Unknown Source)
at com.google.android.gms.internal.zzbtf.zzay(Unknown Source)
at com.google.android.gms.internal.zzbtf.zzay(Unknown Source)  
at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
at com.trending.trendingadmin.fragment.WriteReview.uploadReviews(WriteReview.java:165)

When I remove this public ArrayList<Reviews> reviewsList = new ArrayList<>(); reviewsList in POJO Class data saved in Firebsae database perfectly.

Anyone know help me to solve this issue.

Update

I am Solved this problem by Two Ways. Suggestion given by OlegOsipenko

1) Make the inner class as static

2) Moving the inner class to another file.

Now its work well and good as i expected.

like image 333
Yugesh Avatar asked Oct 18 '22 10:10

Yugesh


1 Answers

As your Logcat error says Keys must not contain '/', '.', '#', '$', '[', or ']', which means that each one of this symbols is not allowed as a key in Firebase database.

In order to write data objects to your Firebase database, in stead of using a List, i suggest you to use a Map. You need also to make this change in your pojo class. To save data, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference namesRef = rootRef.child("names");
Map<String, Object> map = new HashMap<>();
map.put("nameId1", "John");
map.put("nameId2", "Tim");
map.put("nameId3", "Sam");
//and os on
namesRef.updateChildren(map);

Hope it helps.

like image 200
Alex Mamo Avatar answered Oct 21 '22 07:10

Alex Mamo