在android studio中使用firebase搜索

8hhllhi2  于 2023-01-09  发布在  Android
关注(0)|答案(1)|浏览(103)

我尝试搜索该特定条目,但我得到了一个错误。重要的是要注意,RecylerView的代码是正确的,确实只在搜索中显示元素。我得到了一个错误,我不知道在哪里:

recyclerView= findViewById(R.id.recycleview);
        getmRootFLY = FirebaseDatabase.getInstance().getReference("FLY");
        list = new ArrayList<>();
        arrayList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new MyAdapter(this,list);
        recyclerView.setAdapter(myAdapter);
        getmRootFLY.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot: snapshot.getChildren())
                {
                    Fly fly = dataSnapshot.getValue(Fly.class);
                    list.add(fly);
                }
                myAdapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

\\search func.................................

                DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("FLY").child("-NJzbEcGdb8_fvLxWFJk");
                ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                    {
                            list.clear();
                            for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                            {
                                Fly fly = dataSnapshot1.getValue(Fly.class);
                                list.add(fly);
                            }
                            myAdapter.notifyDataSetChanged();
                            
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
            }
        });
    }
}

//worng in console..............................
 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.newp.Fly
        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.example.newp.MainActivity$4$1.onDataChange(MainActivity.java:150)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7872)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

8gsdolmq

8gsdolmq1#

您将收到以下错误:
com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.newp.Fly类型
因为当你创建一个指向db/FLY/-NJzbEcGdb8_fvLxWFJk的引用并循环遍历结果时,你得到的不是Fly对象,而是字符串,所以会出现错误。如果你想得到FLY节点下存在的所有对象并将它们添加到一个列表中,那么你必须删除对.child("-NJzbEcGdb8_fvLxWFJk")的调用。所以你的引用应该如下所示:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("FLY");
ref.addValueEventListener(/* ... /*);
    • 编辑:**
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("FLY");
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
            String nameFly = dataSnapshot1.child("nameFly").getValue(String.class);
            Log.d(TAG, nameFly);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.d("TAG", error.getMessage()); //Never ignore potential errors!
    }
});

相关问题