创建Arraylist并将项目添加到Listview

时间:2011-10-20 21:44:16

标签: android listview arraylist

我使用JSoup解析了几个项目,我希望将其添加到列表视图中,最好通过像这样的arraylist:

End date: 08-10-2012
Left: € 38,50

任何人都可以帮助我吗? 提前谢谢。

JSoup as System.out.println:

的结果
End date: // td:eq(0)
Left:     // td:eq(0)
08-10-2012  // td:eq(1)
€ 38,50     // td:eq(1)

我的代码:

@Override 
    protected void onPostExecute(String result) { 
        //publishProgress(false); 
        TextView tv = (TextView)findViewById(R.id.lbl_top);
        ListView kp = (ListView)findViewById(R.id.kpn);
        tv.setText("");

        Document doc = Jsoup.parse(kpn);
        Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)");
        Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)"); 

        for (Element tdFromSecondColumn : tdsFromSecondColumn) { 
            System.out.println(tdFromSecondColumn.text()); 
            tv.setText(tdFromSecondColumn.text());
            //kp.setAdapter(adapter);
        } 
        for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) { 
            System.out.println(tdFromSecondColumn1.text());
        }

编辑:

@Override 
    protected void onPostExecute(String result) { 
        //publishProgress(false); 
        // create the grid item mapping
        ListView kp = (ListView)findViewById(R.id.kpn);

        String[] from = new String[] {"col_1", "col_2"};
        int[] to = new int[] { R.id.lbl_password, R.id.lbl_result };

        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();

        TextView tv = (TextView)findViewById(R.id.lbl_top);
        tv.setText("");

        Document doc = Jsoup.parse(kpn);
        Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)");
        Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)"); 

        for (Element tdFromSecondColumn : tdsFromSecondColumn) {
            map.put("col_1", tdFromSecondColumn.text()); 
            fillMaps.add(map);

            System.out.println(tdFromSecondColumn.text()); 
            tv.setText(tdFromSecondColumn.text());
            //kp.setAdapter(adapter);
        } 
        for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {
            map.put("col_2", tdFromSecondColumn1.text());
            fillMaps.add(map);

            System.out.println(tdFromSecondColumn1.text());
        }

        SimpleAdapter adapter = new SimpleAdapter(AndroidLogin.this, fillMaps, R.layout.main, from, to); 
        kp.setAdapter(adapter);

App崩溃,Logcat:

ERROR/AndroidRuntime(9105): java.lang.IllegalArgumentException: Object must not be null

2 个答案:

答案 0 :(得分:0)

您应该尝试使用SimpleAdapter作为列表适配器。只需将arraylist中的值映射到列表中每行所用视图的相应部分即可。这是使用SimpleAdapter的example。这是example使用不同类型的适配器ArrayAdapter,可能对您更有用。

答案 1 :(得分:0)

我也建议使用SimpleAdapter;当我需要学习这个适配器时,我会看到这个做得很好example

您可以轻松地重复使用已编写的代码,因为它非常相似。