gpt4 book ai didi

Android/Firebase - 无法将 java.util.ArrayList 类型的对象转换为类型 **我的对象**

转载 作者:行者123 更新时间:2023-11-29 17:13:24 25 4
gpt4 key购买 nike

我正在尝试获取"template"下每个节点的值。

enter image description here

例如,“eght”(只是随机测试名称)包含一个 ArrayList 的 ArrayList。所以,ArrayList<.ArrayList>.

我只需要获取一次值,所以我使用单个事件监听器:

mTemplateRef.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for(DataSnapshot templateSnapshot : dataSnapshot.getChildren()){

// I've tried a million different things here, but all result in the error
// This is the error line
MapFoo mapFoo = templateSnapshot.getValue(MapFoo.class);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
// ...
}
});

我总是这样结束:

Can't convert object of type java.util.ArrayList to type com.appname.template_editor.MapFoo

最后一部分是我尝试的任何对象。

这是 MapFoo:

顺便说一句,我正在使用 Map,因为当我调试时,节点似乎是 HashMap 形式。我也尝试了一个 HashMap 对象,但有同样的错误。我也尝试了其他几个对象来尝试使其无法成功运行。

public class MapFoo {
public Map<String, ArrayList> mapList;

public MapFoo(){

}

public Map getMapList(){
return mapList;
}

public MapFoo(Map<String, ArrayList> mapList){
this.mapList = mapList;
}

我尝试的另一种变体:

public class MasterListTemplateClass {

public String templateName;
public ArrayList<ArrayList> arrayList;

public MasterListTemplateClass() {
}

public MasterListTemplateClass(String templateName, ArrayList<ArrayList> arrayList) {
this.templateName = templateName;
this.arrayList = arrayList;
}

public String getName() {
return templateName;
}

public ArrayList<ArrayList> getList() {
return arrayList;
}

我只是无法让其中的任何一个发挥作用。我只需要数据库中的值!

最佳答案

而不是像 0,1,2 这样的数字 ID 给他们一个名字,以便能够塑造你的模型 因为我不确定是否可以制作带有数字名称的模型。

例如将 0,1,2 分别命名为天数、标题、维度并创建以下模型

public class MyModel{

public String days;
public String title;
public String dimen;

public MyModel(){

}

public String getDays(){
return this.days;
}

public String getTitle(){
return this.title;
}

public String getDimen(){
return this.dimen;
}
}

然后调用 2 个 snapshot.getChildren() 通过 2 个循环到达这个模型

 for(DataSnapshot templateSnapshot : dataSnapshot.getChildren()){
for(DataSnapshot snap : templateSnapshot.getChildren()){


MyModel model = snap.getValue(MyModel.class);
}
}
...

关于Android/Firebase - 无法将 java.util.ArrayList 类型的对象转换为类型 **我的对象**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39925871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com