2015-06-09 53 views
14

我无法从Firebase中检索列表。我有麻烦保存它,但只要我尝试投dataSnapshot.getValue()到ArrayList的我的应用程序崩溃,从而异常:如何从Android的Firebase中检索列表对象

HashMap中不能被强制转换为ArrayList的

但是,当我试图将其转换为一个HashMap,它也崩溃,给人异常:

的ArrayList不能被强制转换为HashMap的

请帮忙!这里是创造该问题的代码:我想要检索的火力地堡中的所有数据作为一个列表

Fire.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     List<TaskDes> td = (ArrayList<TaskDes>) dataSnapshot.getValue() 
     notifyDataSetChanged(); 
    } 

    @Override 
    public void onCancelled(FirebaseError firebaseError) { 

    } 
}); 

firebase object screen shot

。该类TaskDes包含三个字段:

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

列表不幸的是没有足够的代码在这里对我帮助您排除故障。积极的一面:Firebase的AndroidChat示例应用程序可以完成您正在寻找的所有功能以及更多功能。看看它在这里:https://github.com/firebase/AndroidChat/tree/master/app/src/main/java/com/firebase/androidchat。我最近在Firebase Google群组中解释了一些内部消息:https://groups.google.com/d/msg/firebase-talk/OCoQPvpgl1U/Nli_OywG7LYJ –

+0

谢谢我会试一试,另外一件事我想问一下如果我将hashmap 存储在firebase上,那么当从firebase中检索它时,我可以直接将datasnapshot.getValue()转换为hashmap ?? –

+0

是的,那是可能的。但只有在你正确模拟Task类的时候。看看这个例子。你的'Task'应该看起来像https://github.com/firebase/AndroidChat/blob/master/app/src/main/java/com/firebase/androidchat/Chat.java –

回答

25

你需要创建一个GenericTypeIndicator对象传递的参数DataSnapshot.getValue()

代码:

GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {}; 

List<String> yourStringArray = dataSnapshot.getValue(t); 
+9

如果列表在另一个对象内呢? –

+0

@GregEnnis你知道如何解决它,如果列表是在另一个对象内? – malhobayyeb

+0

@malhobayyeb nope从来没有找到一个好办法做 –

6

模型

public class TaskDes { 
    private boolean done; 
    private String taskDescription; 
    private String taskTitle; 

    public TaskDes() { 
    } 

    public boolean isDone() { 
     return done; 
    } 

    public void setDone(boolean done) { 
     this.done = done; 
    } 

    public String getTaskDescription() { 
     return taskDescription; 
    } 

    public void setTaskDescription(String taskDescription) { 
     this.taskDescription = taskDescription; 
    } 

    public String getTaskTitle() { 
     return taskTitle; 
    } 

    public void setTaskTitle(String taskTitle) { 
     this.taskTitle = taskTitle; 
    } 

} 

你需要创建一个GenericTypeIndicator对象传递的参数DataSnapshot.getValue()

在活动

private static final String TAG=MainActivity.class.getSimpleName(); 
private FirebaseDatabase database; 
private DatabaseReference myRef=null; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    database=FirebaseDatabase.getInstance(); 
    myRef=database.getReference("ADD_YOUR_REFERECE"); 


    myRef.addValueEventListener(new ValueEventListener(){ 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot){ 
      /* This method is called once with the initial value and again whenever data at this location is updated.*/ 
      long value=dataSnapshot.getChildrenCount(); 
      Log.d(TAG,"no of children: "+value); 

      GenericTypeIndicator<List<TaskDes>> genericTypeIndicator =new GenericTypeIndicator<List<TaskDes>>(){}; 

      List<TaskDes> taskDesList=dataSnapshot.getValue(genericTypeIndicator); 

      for(int i=0;i<taskDesList.size();i++){ 
      Toast.makeText(MainActivity.this,"TaskTitle = "+taskDesList.get(i).getTaskTitle(),Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     public void onCancelled(DatabaseError error){ 
      // Failed to read value 
      Log.w(TAG,"Failed to read value.",error.toException()); 
     } 
    }); 
} 
0

有点晚,但在任何情况下,其他人需要这个。

如果列表位于另一个对象内部。

对象

public class Question { 

    public Date date; 
    public String questionNumber; 
    public String questionText; 
    public QuestionType questionType; 
    public String multipleSelection1; 
    public String multipleSelection2; 
    public String multipleSelection3; 

    public Question() { 
     // Default constructor required for calls to DataSnapshot.getValue(User.class) 
    } 
} 

然后让你的问题的对象数组

GenericTypeIndicator<List<Question>> t = new GenericTypeIndicator<List<Question>>() {}; 
List<Question> questionList = dataSnapshot.getValue(t); 
0

使包含为您的商品列表中的其他项目: 这是您的项目:

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

这是清单项目

 class TaskDesList { // definition 
       private ArreyList<TaskDes> yourlist 
      } 

     public TaskDesList(){ 
     } 

     public ArrayList<TaskDes> getYourlist() { 
      return yourlist; 
     } 

,并调用一个事件监听时

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

和现在的“yourlist”是包含了所有你的“TaskDes”项目

相关问题