简体   繁体   English

使用 firebase 在 android 工作室中搜索

[英]search in android studio with firebase

I'm trying to search for that specific entry but I'm getting an error It is important to note that the code of the RecylerView is correct and indeed displays elements only in the search I get an error and I don't know where:我正在尝试搜索该特定条目,但出现错误重要的是要注意 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)

消防基地

You're getting the following error:您收到以下错误:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.newp.Fly com.google.firebase.database.DatabaseException:无法将类型 java.lang.String 的 object 转换为类型 com.example.newp.Fly

Because when you're creating a reference that points to db/FLY/-NJzbEcGdb8_fvLxWFJk and you're looping through the results, you aren't getting Fly objects but strings, hence the error.因为当您创建指向db/FLY/-NJzbEcGdb8_fvLxWFJk的引用并且循环遍历结果时,您得到的不是Fly对象而是字符串,因此会出现错误。 If you want to get all objects that exist under the FLY node and add them to a list, then you have to remove the call to .child("-NJzbEcGdb8_fvLxWFJk") .如果要获取FLY节点下存在的所有对象并将它们添加到列表中,则必须删除对.child("-NJzbEcGdb8_fvLxWFJk")的调用。 So your reference should look like this:所以你的参考应该是这样的:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("FLY");
ref.addValueEventListener(/* ... /*);

Edit:编辑:

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

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

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