How to save an array of data in a Firebase database with Android?

I'm new to Firebase Database , I just want to save an array of data in a Firebase database. when I try to save it, an error is displayed. Below I mentioned the POJO Class and Logcat error while saving data.

POJO Code Class

@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;
    }
}

Data storage

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 delete this public ArrayList<Reviews> reviewsList = new ArrayList<>();reviewList in the POJO Class, the data stored in the Firebsae database is excellent.

Does anyone know help me solve this problem.

Update

I solved this problem in two ways. Offer given by Oleg Osipenko

1) Make the inner class static

2) Moving the inner class to another file.

, .

+4
2

.setValue() , . .

Firebase ref = new Firebase("<my-firebase-app>/names"):
String[] names = {"John","Tim","Sam","Ben"};
List nameList = new ArrayList<String>(Arrays.asList(names));
// Now set value with new nameList
ref.setValue(nameList);

Firebase.

+1

Logcat Keys must not contain '/', '.', '#', '$', '[', or ']', , Firebase database.

Firebase, List, Map. pojo. , :

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);

, .

0

Source: https://habr.com/ru/post/1679584/


All Articles