2011-10-20 98 views
1

我通过这样一个ArrayList解析几个项目与JSoup我想添加到列表视图,可取:创建ArrayList和添加项目的ListView

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 

回答

0

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

+0

Hi Kurtis, Thx回复。 你能举一个小例子代码吗? – Lars

+0

你有没有看到我的答案中包含的例子,还是它很大? –

+0

是的,我看到了,但我不明白。 我已经有一些代码,请在我编辑的代码中找到它。 也许你可以纠正它? – Lars

0

我也建议SimpleAdapter;看到这个做得很好example当我需要学习这个适配器的时候,我想到了。

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

+0

嗨Mangusto, 感谢您的回复,我已经找到它,但在代码SimpleAdapter adapter = new SimpleAdapter(this,fillMaps,R.layout.grid_item,from,to);我得到一个错误constrcutor未定义。 请参阅我编辑的代码。 你能帮忙吗? – Lars

+0

尝试定义fillMaps为 'ArrayList > fillMaps = new ArrayList >(); ' – Mangusto

+0

嗨Mangusto, 我已经解决了它,不应该:新的SimpleAdapter(this,..,..),但新的SimpleAdapter(AndroidLogin.this,..,..) Thx无论如何。 – Lars