如何在listview中重复项目

时间:2014-03-31 11:01:44

标签: android excel listview arraylist hashmap

我有来自exel的列表视图:

try {

       Workbook wb = WorkbookFactory.create(getAssets().open("bos.xls"));
       Sheet sheet = wb.getSheetAt(0);
       for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
       Row row = sheet.getRow(i);
              Cell form1 = row.getCell(0);
              Cell form2 = row.getCell(1);
              Cell form3 = row.getCell(2);

              IrrList = new ArrayList<HashMap<String, Object>>();

              HashMap<String, Object> hm;

            hm = new HashMap<String, Object>();
            hm.put(Form1, form1.getStringCellValue());
            hm.put(Form2, form2.getStringCellValue());
            hm.put(Form3, form3.getStringCellValue());
            IrrList.add(hm);

              SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
            R.layout.list_item, new String[] { Form1,  Form2, Form3 },
                                   new int[] { R.id.text1, R.id.text2, R.id.text3 });   
                listView.setAdapter(adapter);
                listView.setTextFilterEnabled(true);  
         }

         } catch(Exception ex) {
             return;
         }

当我使用它时,我的列表视图中有一个值 我怎么能纠正它?

我试图在&#34; HashMap hm;&#34;:

之前添加它
  for(int i1 = 0, l = IrrList.size(); i1 < l; i1++){
"I dont know what write here"
    }

1 个答案:

答案 0 :(得分:0)

试试这个..

在for循环之前初始化ArrayList ,并在for循环之后设置listview adapter

IrrList = new ArrayList<HashMap<String, Object>>();
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
   Row row = sheet.getRow(i);
          Cell form1 = row.getCell(0);
          Cell form2 = row.getCell(1);
          Cell form3 = row.getCell(2);              

          HashMap<String, Object> hm;

        hm = new HashMap<String, Object>();
        hm.put(Form1, form1.getStringCellValue());
        hm.put(Form2, form2.getStringCellValue());
        hm.put(Form3, form3.getStringCellValue());
        IrrList.add(hm);             
     }
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
        R.layout.list_item, new String[] { Form1,  Form2, Form3 },
                               new int[] { R.id.text1, R.id.text2, R.id.text3 });   
            listView.setAdapter(adapter);
            listView.setTextFilterEnabled(true);