我向Firebase添加了一个ArrayList,但我不知道如何取回它。
mDatabase.child("list").child("person").setValue(myValues);
//This is how I added it to my firebase
我试过的是这个。这是我的错误消息
com.google.firebase.database.DatabaseException:无法将java.util.ArrayList类型的对象转换为类型Item
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot data : snapshot.getChildren()) {
GenericTypeIndicator<HashMap<String, Item>> t = new GenericTypeIndicator<HashMap<String, Item>>() {};
HashMap<String, Item> beans = data.getValue(t);
Set set = beans.entrySet();
Iterator i = set.iterator();
// Display elements
while (i.hasNext()) {
Map.Entry me = (Map.Entry) i.next();
Toast.makeText(getContext(), me.getKey() + ": ", Toast.LENGTH_LONG).show();
Toast.makeText(getContext(), me.getValue().toString(), Toast.LENGTH_LONG).show();
System.out.println(me.getValue());
}
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("The read failed: " ,firebaseError.getMessage());
}
};
mDatabase.addValueEventListener(postListener);
答案 0 :(得分:14)
很抱歉,如果您正在处理此事,我需要做的就是更改目录。
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<ArrayList<Item>> t = new GenericTypeIndicator<ArrayList<Item>>() {};
ArrayList<Item> yourStringArray = snapshot.getValue(t);
Toast.makeText(getContext(),yourStringArray.get(0).getName(),Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("The read failed: " ,firebaseError.getMessage());
}
};
mDatabase.child("list/user1").addValueEventListener(postListener);
答案 1 :(得分:0)
使用此我获得了存储在此对象中的消息列表。
private void lerMessagensFirebase() {
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
final ChatFirebaseAdapter firebaseAdapter = new ChatFirebaseAdapter(mFirebaseDatabaseReference.child("chatmodel"), userModel.getName(), this);
firebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = firebaseAdapter.getItemCount();
int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
rvListMessage.scrollToPosition(positionStart);
}
}
});
rvListMessage.setLayoutManager(mLinearLayoutManager);
rvListMessage.setAdapter(firebaseAdapter);
}
答案 2 :(得分:0)
当返回列表时,这通常是 HashMap<String, ?>
到 ArrayList<?>
...
ArrayList<SomeClass> toArrayList(@NonNull DataSnapshot snapshot) {
GenericTypeIndicator<HashMap<String, SomeClass>> t = new GenericTypeIndicator<HashMap<String, SomeClass>>() {};
HashMap<String, SomeClass> map = snapshot.getValue(t);
ArrayList<SomeClass> items = new ArrayList<>();
if(map != null) {
for (Map.Entry<String, SomeClass> pair : map.entrySet()) {
items.add(pair.getValue());
}
}
return items;
}
我更想知道如何传递通用的 Class<?>
。