简体   繁体   中英

How to get the values of the ListAdapter to a workbook cell

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"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM