[英]search in android studio with firebase
我正在嘗試搜索該特定條目,但出現錯誤重要的是要注意 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)
您收到以下錯誤:
com.google.firebase.database.DatabaseException:無法將類型 java.lang.String 的 object 轉換為類型 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!
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.