无法正确填写listview

时间:2011-10-22 09:00:51

标签: android android-asynctask android-listview jsoup simpleadapter

我从网上获取一些数据,在AsyncTask中使用JSoup进行解析。

我无法正确填写列表视图。

Edit1只填充空字段(..),最后一行的值为€0,00。 Edit2根本没有填满。

..         edit2Text
..         edit2Text
..         edit2Text
€ 0,00     edit2Text
€ 0,00     edit2Text

两个编辑都在test.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<EditText
    android:inputType="textMultiLine"  
    android:id="@+id/editText1"  
    android:layout_height="wrap_content"  
    android:text="edit1"  
    android:layout_width="200dp"
/>

<EditText
    android:layout_height="wrap_content"  
    android:layout_width="110dp"  
    android:id="@+id/editText2"  
    android:text="edit2" 
    android:inputType="textMultiLine" 
    android:layout_marginLeft="10dp"
/>

</LinearLayout>

listview应如下所示:

Einddatum contract: 08-10-2012
Prijs per maand: € 38,50 /mnd
...

我抓住地图和map1,看看他们是否填写正确,他们做了什么,我做错了什么?

10-22 10:35:21.698: I/System.out(8132): Hashmap: {col_1=Einddatum contract:}
10-22 10:35:21.698: I/System.out(8132): Hashmap: {col_1=Prijs per maand:}
10-22 10:35:21.698: I/System.out(8132): Hashmap: {col_1=Einddatum contract:}
10-22 10:35:21.698: I/System.out(8132): Hashmap: {col_1=Prijs per maand:}
10-22 10:35:21.698: I/System.out(8132): Hashmap: {col_1=Nieuw beltegoed:}
10-22 10:35:21.698: I/System.out(8132): Hashmap: {col_1=Tegoed vorige periode:}
10-22 10:35:21.708: I/System.out(8132): Hashmap: {col_1=Tegoed tot 09-11-2011:}
10-22 10:35:21.708: I/System.out(8132): Hashmap: {col_1=}
10-22 10:35:21.708: I/System.out(8132): Hashmap: {col_1=Verbruik sinds nieuw tegoed:}
10-22 10:35:21.708: I/System.out(8132): Hashmap: {col_1=Ongebruikt tegoed:}
10-22 10:35:21.708: I/System.out(8132): Hashmap: {col_1=Verbruik boven bundel:}
10-22 10:35:21.708: I/System.out(8132): Hashmap: {col_1=Verbruik dat niet in de bundel zit*:}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=08-10-2012}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 38,50 /mnd}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=08-10-2012}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 38,50 /mnd}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 54,64}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 17,28}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 71,92}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 5,32}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 66,60}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 0,00}
10-22 10:35:21.718: I/System.out(8132): Hashmap1: {col_2=€ 0,00}

我的代码:

@Override 
    protected void onPostExecute(String result) { 
        // 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.editText1, R.id.editText1 };

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


        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("Hashmap: " + map);

        } 
        for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {
            map1.put("col_2", tdFromSecondColumn1.text());
            fillMaps.add(map1);

            System.out.println("Hashmap1: " + map1);
        }

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

1 个答案:

答案 0 :(得分:1)

试试这个,

@Override 
    protected void onPostExecute(String result) { 
        // 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.editText1, R.id.editText2 };

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

 HashMap<String, String> map;
 HashMap<String, String> map1;


        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 = new HashMap<String, String>();
                  map.put("col_1", tdFromSecondColumn.text()); 
            fillMaps.add(map);

            System.out.println("Hashmap: " + map);

        } 
        for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {
                           map1 = new HashMap<String, String>();                                      map1.put("col_2", tdFromSecondColumn1.text());
            fillMaps1.add(map1);

            System.out.println("Hashmap1: " + map1);
        }

                        for (int i=0;i<fillMap.size();i++) {

               fill_Map.add(fillMap.get(i));
               fill_Map.add(fillMap1.get(i));
}
        SimpleAdapter adapter = new SimpleAdapter(AndroidLogin.this, fill_Map, R.layout.test, from, to); 
        kp.setAdapter(adapter);

让我知道发生了什么......