java - Android ListView 与数组数据库

标签 java android arrays listview baseadapter

我在正确显示 ListView 时遇到问题

如果我声明

String[] cod = {"cod1","cod2", "cod3"};
String[] desc = {"desc1","desc2", "desc3"};
String[] preco = {"1.00","2.00", "3.00"}; 

我的适配器是:

lista = (ListView)findViewById(R.id.list);
lista.setAdapter(new dataListAdapter(cod, desc, preco));

完美运行!

enter image description here

但是如果我从数据库获取数据,ListView 只显示一行。

results = DbHelper.getItemsDao().queryRaw(sqlGetItems);

/*GET ALL RESULTS FROM DATABASE*/
List<String[]> array = results.getResults();    

int count = array.size();

String[] cod = null, desc = null, preco = new String[count];

for(int i = 0; i < count; i++) {
    String[] res = array.get(i);
    cod = new String[]{res[0]} ;
    desc = new String[]{res[1]};
    preco = new String[]{res[3]};
}

lista = (ListView) findViewById(R.id.list);
lista.setAdapter(new dataListAdapter(cod, desc, preco));

enter image description here

最佳答案

尝试这个解决方案:

for(int i = 0; i < count; i++) {
    String[] res = array.get(i);
    cod.add(res[0]) ;
    desc.add(res[1]);
    preco.add(res[3]);
}

关于java - Android ListView 与数组数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24794402/

相关文章:

java - 如何通过 jUnit 使用内部扫描仪测试方法?

Java - 将文本文件的长文本复制到另一个文件

Android 亚马逊 s3 桶

安卓通知动态图标

c++ - 对象数组的插入方法c++

java - 在 javax.script 中加载 javascript 库?

java - LisrView 子项的不同颜色

python - 格式化为整数的 numpy savetxt 不保存零

javascript - 如果为空,如何分配属性

java - 同步部署在不同服务器上的批处理应用程序,仅由一台服务器处理文件