2017-06-19 64 views
4

我是新來的Firebase數據庫,我只想保存Firebase數據庫中的數據數組。當我試圖保存它顯示錯誤。下面我提到了POJO類Logcat保存數據時出錯。如何從Android中保存Firebase數據庫中的數據數組?

POJO類編碼

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

保存數據

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的錯誤

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) 

當我除去叔他的public ArrayList<Reviews> reviewsList = new ArrayList<>(); reviewsList在POJO類完美保存在Firebsae數據庫中的數據。

任何人都知道幫我解決這個問題。

更新

我通過兩種方式解決了這個問題。通過OlegOsipenko

1中給出建議)使內部類爲靜態

2)移動的內類到另一個文件。

現在它的工作很好,並且我期望的很好。

+0

WriteReview.java的第165行是什麼? –

+0

也請提供[mcve]。 –

+0

@Yugesh您的數據不得包含'/','。','#','$','['或']',這意味着您的數據主體或教師或標記或dbUploadReview可能具有該符號。一次只有字符或數字才能找到相同的問題。 –

回答

1

.setValue()方法需要一個List而不是一個Array。您可以使用對象列表。

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

Read the Firebase docs for more information.

0

當你的logcat的錯誤說Keys must not contain '/', '.', '#', '$', '[', or ']',這意味着該符號中的每一個不允許在Firebase database的關鍵。

爲了數據對象寫信給你的火力地堡的數據庫,在使用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

感謝您的建議。我解決了這個問題。 – Yugesh

+0

你用這種方法解決了這個問題嗎? –

+0

不,請參閱我的問題更新。 – Yugesh

相關問題