简体   繁体   English

java.util.HashMap 不能投(android studio, firebase,java)

[英]java.util.HashMap cannot be cast (android studio, firebase,java)

我的 firebase-realtime-database 映像

I've been stuck for two days on this previous segment (Java and Firebase).我在上一个部分(Java 和 Firebase)上被困了两天。

I kept a class-type list in the database but when I try to get it to Firebase I get an error.我在数据库中保留了一个类类型列表,但是当我尝试将其设置为 Firebase 时出现错误。

This is the code:这是代码:

ArrayList<PostDetails_class> cPostList=new ArrayList<PostDetails_class>();
FirebaseDatabase.getInstance().getReference().addValueEventListener( new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot snapshot) {
              cPostList= (ArrayList<PostDetails_class>) snapshot.child( "application_details" ).child( "community_postsList" ).getValue();
          }
          @Override
          public void onCancelled(@NonNull DatabaseError error) {

          }
      } );

This is an attempt to read data from the "cPost_List" list which is a list built from the "PostDetails_class" class.这是从“cPost_List”列表中读取数据的尝试,该列表是从“PostDetails_class”class 构建的列表。 ("community_postsList" is a child that is a list of the "PostDetails_class" class): (“community_postsList”是“PostDetails_class”类的子列表):

PostDetails_class postDetails_class=cPostList.get( 0 );  //line 123 in main activity

the error:错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapp, PID: 1938
    java.lang.ClassCastException: java.util.HashMap cannot be cast to com.example.myapp.PostDetails_class
        at com.example.myapp.MainActivity.makeList(MainActivity.java:123)

When you call getValue() without passing any argument, Firebase returns the native type for the data, which in this case is a Map<String, Object> .当您在不传递任何参数的情况下调用getValue()时,Firebase 返回数据的本机类型,在本例中为Map<String, Object>

You need to tell Firebase what class to return by specifying it in the call to getValue , for example with: getValue(PostDetails_class.class) .你需要告诉 Firebase 什么 class 通过在getValue的调用中指定它返回,例如: getValue(PostDetails_class.class)


Firebase can't return a generic list in the way you're asking it to, because the type of the list is returned. Firebase 无法按照您要求的方式返回通用列表,因为返回了列表的类型。 I typically prefer looping of the child nodes, and extracting them one by one:我通常更喜欢循环子节点,并一一提取它们:

ArrayList<PostDetails_class> cPostList=new ArrayList<PostDetails_class>();
FirebaseDatabase.getInstance().getReference("application_details/community_postsList").addValueEventListener( new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot child: snapshot.getChildren()) {
            PostDetails_class value = child.getValue(PostDetails_class.class);
            cPostList.add(value);
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException();
    }
});

You'll note that I also changed the code to load data from the deeper node, instead of loading the entire database.您会注意到我还更改了代码以从更深的节点加载数据,而不是加载整个数据库。


If you want to get the entire list in one go, that is possible but you will need a generic type indicator for the list.如果您想在一个 go 中获取整个列表,这是可能的,但您需要列表的通用类型指示器。 I recommend reading more on that in these questions about [ firebase-realtime-database][android] generictypeindicator .我建议在有关 [ firebase-realtime-database][android] generictypeindicator的这些问题中阅读更多内容。

ArrayList<PostDetails_class> cPostList=new ArrayList<PostDetails_class>();

Firebase returns HashMap , so you can't receive it into ArrayList Firebase 返回HashMap ,因此您无法将其接收到ArrayList

So you need to change the type of the cPostList to HashMap<Integer, PostDetails_class>所以你需要将cPostList的类型更改为HashMap<Integer, PostDetails_class>

HashMap<Integer, PostDetails_class> cPostList = new HashMap<>();

FirebaseDatabase.getInstance().getReference().addValueEventListener( new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot snapshot) {
              cPostList= (HashMap<Integer, PostDetails_class>) snapshot.child( "application_details" ).child( "community_postsList" ).getValue();
          }
          @Override
          public void onCancelled(@NonNull DatabaseError error) {

          }
      } );

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

相关问题 无法将java.util.HashMap强制转换为java.util.ArrayList - java.util.HashMap cannot be cast to java.util.ArrayList java.util.hashmap无法转换为java.util.list - java.util.hashmap cannot be cast to java.util.list Android java.util.hashmap无法转换为java.util.list - Android java.util.hashmap cannot be cast to java.util.list 错误SessionMap无法转换为java.util.HashMap - error SessionMap cannot be cast to java.util.HashMap SDN4:ClassCastException:无法将java.util.HashMap强制转换为[EntityNode] - SDN4: ClassCastException: java.util.HashMap cannot be cast to [EntityNode] FlexJson 错误:ClassCastException:java.util.HashMap 无法转换为类 - FlexJson Error : ClassCastException: java.util.HashMap cannot be cast to class 无法在android putExtra方法中将java.util.hashmap $ values强制转换为java.io.serializable - java.util.hashmap$values cannot be cast to java.io.serializable in android putExtra method Android java.util.HashMap无法转换为java.lang.String - Android java.util.HashMap cannot be cast to java.lang.String java.lang.ClassCastException:java.util.HashMap $ EntrySet无法转换为java.util.HashSet - java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.HashSet java.lang.ClassCastException: java.util.HashMap$EntrySet 不能转换为 java.util.Map$Entry - java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.Map$Entry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM