我在firebase数据库上传了ArrayList
个对象,当我下载ArrayList
并输入ArrayList
值时,我收到消息“HashMap无法转换为java.util。数组列表”。但是我上传ArrayList
,下载ArrayList
,输入ArrayList
值,那么为什么错误会说HashMap?
public class TileContentFragment extends Fragment {
ArrayList<Item> mItems=new ArrayList<>();
private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
private DatabaseReference mDatabase;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Initialize Firebase Auth and Database Reference
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference();
RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.recycler_view, container, false);
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext(),mItems);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
// Set padding for Tiles
int tilePadding = getResources().getDimensionPixelSize(R.dimen.tile_padding);
recyclerView.setPadding(tilePadding, tilePadding, tilePadding, tilePadding);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
return recyclerView;
}
@Override
public void onStart() {
super.onStart();
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChild("ItemsArray")) {
final ValueEventListener userListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mItems = new ArrayList<>((ArrayList) dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("AppInfo", "onCancelled: ",databaseError.toException());
}
};
mDatabase.child("ItemsArray").addValueEventListener(userListener);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView picture;
public TextView name;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.item_tile, parent, false));
picture = (ImageView) itemView.findViewById(R.id.tile_picture);
name = (TextView) itemView.findViewById(R.id.tile_title);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_POSITION, getAdapterPosition());
context.startActivity(intent);
}
});
}
}
/**
* Adapter to display recycler view.
*/
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of Tiles in RecyclerView.
private static final int LENGTH = 18;
private final String[] mPlaces;
private ArrayList<Item> mPlacePictures;
public ContentAdapter(Context context,ArrayList<Item> mPlacePictures) {
Resources resources = context.getResources();
mPlaces = resources.getStringArray(R.array.places);
this.mPlacePictures=mPlacePictures;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Picasso.with(holder.picture.getContext()).load(mPlacePictures.get(position).getProfilePic()).into(holder.picture);
// holder.picture.setImageDrawable(mPlacePictures[position % mPlacePictures.length]);
holder.name.setText(mPlaces[position % mPlaces.length]);
}
@Override
public int getItemCount() {
return mPlacePictures.size();
}
}
}
public class Item {
private String user;
private String name;
private String desription;
private int price;
private ArrayList<String> url;
private String profilePic;
public Item(){}
public Item(String user,String name, String desription, int price, ArrayList<String> url){
this.user=user;
this.name=name;
this.desription=desription;
this.price=price;
this.url=url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesription() {
return desription;
}
public void setDesription(String desription) {
this.desription = desription;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public ArrayList<String> getUrl() {
return url;
}
public void setUrl(ArrayList<String> url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getProfilePic() {
return profilePic;
}
public void setProfilePic(String profilePic) {
this.profilePic = profilePic;
}
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.packtpub.materialdesign4, PID: 713
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
at com.packtpub.materialdesign4.TileContentFragment$1$1.onDataChange(TileContentFragment.java:82)
at com.google.android.gms.internal.zzakg.zza(Unknown Source)
at com.google.android.gms.internal.zzalg.zzcxk(Unknown Source)
at com.google.android.gms.internal.zzalj$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
答案 0 :(得分:0)
在此处查看DataSnapshot
的文档:https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html#getValue--
getValue()以本机类型的形式返回此快照中包含的数据。返回的可能类型是:
Boolean
Long
Double
Map <String, Object>
List<Object>
方法dataSnapshot.getValue()
正在返回Map<String, Object>
而不是您期望的List<Object>
。
首先投射到Map
,然后从结果中创建ArrayList
可以解决您的问题
mItems = new ArrayList(((Map<String, Item>) dataSnapshot.getValue()).values());