firebase回收器视图仅显示第三个子项上的错误

e4eetjau  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(438)

我正在做一个基于firebase实时数据库的android应用程序。我正在尝试在回收器视图中获取数据。
这是数据库结构:

我可以显示“看不见”以及“0”在我的回收视图。但我实际上需要在我的回收器视图中使用“eeee”,并得到以下错误:

2020-12-16 00:44:14.002 30685-30685 E/Toast: setGravity() shouldn't be called on text toasts, the values won't be used
2020-12-16 00:44:17.897 30685-30685 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.my app name, PID: 30685
    com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.my app name.model.model
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
        at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
        at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
        at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
        at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
        at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
2020-12-16 00:44:17.900 30685-30685 E/AndroidRuntime:     at com.android.internal.policy.DecorView.onLayout(DecorView.java:784)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3470)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2938)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:731)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

这是我用来将所需子对象提取到回收器视图中的java代码:

myRef = myfire.getReference().child("Data").child("Unseen");

        options1 = new FirebaseRecyclerOptions.Builder<model>()
                .setQuery(myRef, model.class)
                .build();
        FirebaseRecyclerAdapter<model, holder1> adapter = new FirebaseRecyclerAdapter<model, holder1>(options1) {
            @SuppressLint("SetTextI18n")
            @Override
            protected void onBindViewHolder( holder1 holder,   final int i, final model model) {
                final String set =getRef(i).getKey();
                holder.tvTopic.setText(set);

            }

            @NonNull
            @Override
            public holder1 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item1, parent, false);
                return new holder1(v);
            }
        };

我的模型课是这样的:

public class model {

    public model() {
    }
}

我想重复一遍,无论何时,我都可以获取第二个未看到的子级和第四个子级0,但无法获取第三个子级eeee。我看不出问题出在哪里!

z3yyvxxp

z3yyvxxp1#

传入模型类时,firebaseui使用每个节点的值填充该模型。因为您希望在ui中使用键,所以您需要使用一个定制的快照解析器 setQuery 超载:

options1 = new FirebaseRecyclerOptions.Builder<model>()
        .setQuery(myRef, new SnapshotParser<String> {
            String parseSnapshot(@NonNull DataSnapshot snapshot) {
                return snapshot.getKey();
            }
        })
        .build();
qq24tv8q

qq24tv8q2#

看起来firebase将这些项目转换为 List (因为它们看起来像一个数组),所以您需要:

new FirebaseRecyclerOptions.Builder<model>()
        .setQuery(myRef, List.class)
        .build();
qyzbxkaa

qyzbxkaa3#

我找到了问题的解决办法。
当子项为零(即0)时,firebase recycler视图不显示上一个键。
我将firebase实时数据库中的第四个子级“0”替换为“00”。
数据>>看不见>>eeee>>00>>英语速记
令人惊讶的是,它工作得很好。

相关问题