如何获得ListAdapter值到工作簿细胞 [英] How to get the values of the ListAdapter to a workbook cell

查看:230
本文介绍了如何获得ListAdapter值到工作簿细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建从我的应用程序的工作簿。我从sqlite的数据的ArrayList中,然后用ListAdapter进行查看。我可以保存到工作簿就好了。行显示的数据,但它们显示出的是从像Personfirstname = John和Personlastname = Doe的都在一个小区中的查询的原始数据。这不是我想要的。我想是太见约翰小区1,李四在小区2也简行单元2和母鹿的2行2。我知道我需要做我的ListAdapter东西,我只是不知道该怎么细胞2做的。

下面是我的code(我使用apache.poi JAR)

 的ArrayList<&HashMap的LT;字符串,字符串>>测试=控制器.getAllpersons(值1,值2);ListAdapter适配器1 =新SimpleAdapter(Persons.this,测试,R.layout.rowsales,
新的String [] {NamesId,personfirstname,personlastname},
新的INT [] {R.id.NamesId,R.id.personfirstname,R.id.personlastname});
的for(int i = 0; I< test.size();我++){行ROW2 = sheet1.createRow((短)I + 3);
C = row2.createCell(0);
的System.out.println(test.get(I));
c.setCellValue(test.get(ⅰ)的ToString());
c.setCellStyle(多个);
}


解决方案

您的问题实际上不是与POI code(大部分)...

您的问题与这些两行:

 的ArrayList<&HashMap的LT;字符串,字符串>>测试=控制器.getAllpersons(值1,值2);c.setCellValue(test.get(ⅰ)的ToString());

您变量测试是哈希映射列表。该地图将通过键索引,你想要的东西将在值。但是,你则忽略了地图查找,只是取了一大堆+变成一个字符串。

假设的String [] 传递给 SimpleAdapter 是地图的钥匙,那么你' ð需要做的是:

 工作簿WB =新HSSFWorkbook();
纸张S1 = wb.createSheet();
的for(int i = 0; I< test.size();我++){
    字符串名字=
    行r = s1.createRow(ⅰ);    地图<字符串,字符串> personData = test.get(ⅰ);    细胞FN = r.createCreateCell(0,Cell.CELL_TYPE_STRING);
    fn.setCellValue(data.get(personfirstname));    细胞LN = r.createCreateCell(1,Cell.CELL_TYPE_STRING);
    ln.setCellValue(data.get(personlastname));
}

I am creating a workbook from my app. I get the Arraylist of data from sqlite and then use a ListAdapter to view it. I can save to the workbook just fine. The rows show the data but what they show is the raw data from the query like Personfirstname= John, and Personlastname = Doe all in one cell. That is not what I want. What I want is too see John in cell 1, Doe in cell 2 also Jane in row 2 of cell 2 and doe in cell 2 of row 2. I know I need to do something with my ListAdapter I just don't know what to do.

Here is my code(I'm using apache.poi jar)

ArrayList<HashMap<String, String>> test = controller .getAllpersons(value1,value2);

ListAdapter adapter1 = new SimpleAdapter(Persons.this, test, R.layout.rowsales,
new String[] { "NamesId", "personfirstname","personlastname }, 
new int[] {R.id.NamesId, R.id.personfirstname,R.id.personlastname});


for (int i = 0; i < test.size(); i++) {

Row row2 = sheet1.createRow((short) i+3);
c = row2.createCell(0);
System.out.println(test.get(i));
c.setCellValue(test.get(i).toString());
c.setCellStyle(s);
}

解决方案

Your problem isn't actually with the POI code (mostly)...

Your problem are with these two lines:

ArrayList<HashMap<String, String>> test = controller .getAllpersons(value1,value2);

c.setCellValue(test.get(i).toString());

Your variable test is a list of hash maps. The maps will be indexed by key, and the things you want will be in the value. However, you're then ignoring the map lookup, and just fetching the whole lot + turning into a string.

Assuming that the String[] you pass to SimpleAdapter is the keys of the map, then what you'd need to do is:

Workbook wb = new HSSFWorkbook();
Sheet s1 = wb.createSheet();
for (int i=0; i<test.size(); i++) {
    String firstname = 
    Row r = s1.createRow(i);

    Map<String,String> personData = test.get(i);

    Cell fn = r.createCreateCell(0, Cell.CELL_TYPE_STRING);
    fn.setCellValue(data.get("personfirstname"));

    Cell ln = r.createCreateCell(1, Cell.CELL_TYPE_STRING);
    ln.setCellValue(data.get("personlastname"));
}

这篇关于如何获得ListAdapter值到工作簿细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆