【问题标题】:Create Arraylist and add items to Listview创建 Arraylist 并将项目添加到 Listview
【发布时间】:2011-10-20 21:44:15
【问题描述】:

我用 JSoup 解析了几个项目,我想将它们添加到列表视图中,最好通过这样的数组列表:

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

谁能帮帮我? 提前谢谢你。

JSoup 的结果为 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);

应用程序崩溃,Logcat:

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

【问题讨论】:

    标签: android listview arraylist


    【解决方案1】:

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

    【讨论】:

    • 嗨,Kurtis,谢谢您的回复。能给个小例子代码吗?
    • 您没有看到我在答案中包含的示例,还是太大了?
    • 是的,我看到了,但我没有做对。我已经有一些代码,请在我编辑的代码中找到它。也许你可以纠正它?
    • 我现在在回答中加入了更多示例。
    • 嗨,Kurtis,我快到了,但我无法编译,好像收到消息 Constructor SimpleAdapter is undefined??查看我编辑的代码。
    【解决方案2】:

    我也建议使用 SimpleAdapter;看到这个制作精良的example 当我需要学习这个适配器时,我就跟随了。

    并且您可以轻松地重用您已经编写的代码,因为它们非常相似。

    【讨论】:

    • 嗨 Mangusto,谢谢你的回复,我已经找到了,但是在代码 SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);我得到一个未定义的错误构造函数。请查看我编辑的代码。你能帮忙吗?
    • 尝试将 fillMaps 定义为 ` ArrayList> fillMaps = new ArrayList>(); `
    • 嗨 Mangusto,我已经解决了,不应该是:new SimpleAdapter(this, .., ..) 而是 new SimpleAdapter(AndroidLogin.this, .., ..) 无论如何。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多