【问题标题】:How to repeat item in listview如何在列表视图中重复项目
【发布时间】:2014-03-31 11:01:43
【问题描述】:

我有来自 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;
         }

当我使用它时,我的列表视图中只有一个值。
怎么改?

我尝试在“HashMap hm;”之前添加这个:

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

【问题讨论】:

    标签: android excel listview arraylist hashmap


    【解决方案1】:

    试试这个..

    在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);  
    

    【讨论】:

    • 感谢您的快速回复! =)我知道这很简单)
    • @user3349698 很高兴它有帮助。快乐的编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2016-06-02
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多