How to Retrieve a List object from the firebase in android

后端 未结 5 1961
无人共我
无人共我 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 03:54

    Make another item that contains a list for your item: This is your item:

    class TaskDes { // definition
        boolean done
        String taskDescription
        String taskTitle
    }
    

    This is the list item

            class TaskDesList { // definition
                  private  ArreyList yourlist
                }
    
          public TaskDesList(){
             }
    
            public ArrayList getYourlist() {
                return yourlist;
            }
    

    and when calling an EventListener

    ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    yourlist.clear();
                 taskDesList=dataSnapshot.getValue(TaskDesList.class);
                    if (taskDesList!=null) {
                        yourlist= taskDesList.getYourlist();
                    }
    }
    

    and now "yourlist" is a list that contains all of your "TaskDes" items

提交回复
热议问题