How to Retrieve a List object from the firebase in android

后端 未结 5 1963
无人共我
无人共我 2020-11-30 03:13

I am having trouble retrieving a List from the Firebase. I have no trouble storing it, but as soon as I try to cast dataSnapshot.getValue() to ArrayList my app crashes, givi

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 04:06

    Apparently, the GenericTypeIndicator doesn't work for all List objects particularly when the object contains none primitive types like maps. So, if it didn't work for your use case as it didn't for me, try this alternate solution:

     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
    
        List tDlist = new ArrayList<>();
    
        for (DataSnapshot d: dataSnapshot.getChildren()){
            TaskDes tD = d.getValue(TaskDes.class);
            tDlist.add(tD);
        }
    
        notifyDataSetChanged();
     }
    

    As mentioned in the previous answers make sure your class( like TaskDes in this case) has a public constructor which is empty so the getValue method can deserialize correctly to your java class.

提交回复
热议问题