gpt4 book ai didi

java - 无法检索 Android 自定义类 arraylist 数据

转载 作者:行者123 更新时间:2023-12-02 05:01:24 24 4
gpt4 key购买 nike

我有一个带有 3 个变量的简单自定义 arraylist 类,并且我已将 SQLite 中的数据存储到其中,如下所示:

    List<String> nomess= produtoDAO.getAllProductsId(id);
Cursor allrows = produtoDAO.getAllDataForSpinner(id);

if (allrows.moveToFirst()) {
do {

String ID = allrows.getString(0);
String NAME = allrows.getString(1);
String PLACE = allrows.getString(2);
SpinnerObject data = new SpinnerObject(ID, NAME, PLACE);
nomes.add(data);

} while (allrows.moveToNext());
}

SpinnerObject 类:

封装对象;

public class SpinnerObject {

private String id;
private String nome;
private String id_cliente;

public SpinnerObject(String _id, String _nome, String _id_cliente)
{
_id = id;
_nome = nome;
_id_cliente = id_cliente;
}
}

我想知道如何获取存储的信息并转换为字符串数组列表。我尝试了 nomes.get(position) 但它似乎不起作用(我收到错误 object.SpinnerObject 无法转换为 java.util.List)。

请帮忙!提前致谢!

最佳答案

I get the error object.SpinnerObject cannot be cast to java.util.List

您可能正在尝试将 SpinnerObject 对象强制转换为 List,因为 nomes.get(position) 应该返回 SpinnerObject > 类对象而不是 List

从列表中获取值:

SpinnerObject objSpinner=nomes.get(position);

SpinnerObject类中创建getter方法:

public class SpinnerObject {
...

public String getId(){
return this.id;
}

public String getName(){
return this.nome;
}

}

现在使用 objSpinner.getId()objSpinner.getName() 访问所有值

关于java - 无法检索 Android 自定义类 arraylist 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28259111/

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