简体   繁体   English

在Android上使用Firebase的简单ListView。 JSON问题

[英]Simple ListView using Firebase on Android. Issue with JSON

I want to display a simple list of names in a list view. 我想在列表视图中显示一个简单的名称列表。 But I'm not able to figure out how. 但是我不知道怎么做。 I think I'll not be able to use a modal class since all the values in JSON will have same property called students . 我认为我将无法使用模式类,因为JSON中的所有值都具有称为学生的相同属性。 My JSON data on firebase looks like this on firebse 我在firebase上的JSON数据在firebse上看起来像这样

{
"physicsClass":{
    "students":["John","Frank","Allen"]
},
"chemistryClass":{
    "students":["Gillen","Jason","David East"]
}
}

When I run the it crashes with the following error log: 当我运行它时,它崩溃并显示以下错误日志:

    com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.ArrayList to String
at com.google.android.gms.internal.zzaix.zzaC(Unknown Source)
at com.google.android.gms.internal.zzaix.zzb(Unknown Source)
at com.google.android.gms.internal.zzaix.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.FirebaseListAdapter.parseSnapshot(FirebaseListAdapter.java:113)
at com.firebase.ui.database.FirebaseListAdapter.getItem(FirebaseListAdapter.java:102)
at com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.java:130)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18794)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18794)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Suggest any other way of implementing the above data in a list view / recycler view. 建议在列表视图/回收器视图中以其他方式实现上述数据。

I'm using the firebase-ui sdk to display data in the listview. 我正在使用firebase-ui sdk在列表视图中显示数据。 Below is the code 下面是代码

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference sezRef = ref.child("physics");
studentList = (ListView)findViewById(R.id.studentList);
firebaseListAdapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,phyRef) {
                @Override
                protected void populateView(View v, String model, int position) {
                    TextView textView = (TextView)v.findViewById(android.R.id.text1);
                    textView.setText(model);
                }
            };studentList.setAdapter(firebaseListAdapter);

The error Failed to convert value of type means that when you retrieve you object from Firebase snapshot, you try to get the information as a String but the data in the snapshot is actually an ArrayList . 错误“ Failed to convert value of type意思是,当您从Firebase快照检索对象时,您尝试以String获取信息,但是快照中的数据实际上是ArrayList That is why Firebase throws a DatabaseException . 这就是Firebase抛出DatabaseException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM