使用Jsoup填充ListView(Using Jsoup to populate a ListView)

我试图使用Jsoup解析一个网站,获取我得到的信息并填充ListView 。 HTML看起来像这样:

<ul class="list_view"> <li> <a href="/username/" > <table class="pinner"> <tbody> <tr> <td class="first_td"> <img src="http://myimgurl.com/img.jpg"> </td> <td> <span class="user_name">User Name</span> </td> </tr> </tbody> </table> </a> </li> </ul>

因此,从这个HTML中,我需要从a标签和span.user_name文本中获取href 。 我需要同时使用这两个元素并将它们存储在HashMap (我认为??)现在,我在AsyncTask就像这样(但我不认为我这样做是正确的):

private class MyTask extends AsyncTask<Void, Void, List<HashMap<String, String>>> { @Override protected List<HashMap<String, String>> doInBackground(Void... params) { List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map = new HashMap<String, String>(); try { Document doc = Jsoup.connect("http://myurl.com").get(); Elements formalNames = doc.select("li a table tbody tr td span.user_name"); Elements userNames = doc.select("li a"); for (Element formalName : formalNames) { map.put("col_1", formalName.text()); fillMaps.add(map); System.out.println(formalName.text()); } for (Element userName : userNames) { map.put("col_2", userName.attr("href").toString()); fillMaps.add(map); System.out.println(userName.attr("href").toString()); } } catch (IOException e) { e.printStackTrace(); } return fillMaps; } @Override protected void onPostExecute(List<HashMap<String, String>> result) { String[] from = new String[] {"col_1", "col_2"}; int[] to = new int[] { R.id.text1, R.id.text2 }; ListView _listview = (ListView)findViewById(R.id.listView1); SimpleAdapter _adapter = new SimpleAdapter(FriendsActivity.this, fillMaps, R.layout.friends, from, to); _listview.setAdapter(_adapter); } }

这会成功打印出我想要的信息,但它不会填充ListView 。 我已经尝试过重新安排等等,但仍然没有运气。 我会非常感激任何帮助。

I am trying to parse a website using Jsoup, take the info I get and populate a ListView. The HTML looks like this:

<ul class="list_view"> <li> <a href="/username/" > <table class="pinner"> <tbody> <tr> <td class="first_td"> <img src="http://myimgurl.com/img.jpg"> </td> <td> <span class="user_name">User Name</span> </td> </tr> </tbody> </table> </a> </li> </ul>

So, from this HTML, I need to get the href from the a tag, and also the span.user_name text. I need to take both of these elements and store them in a HashMap (I think??) Right now, I have this in an AsyncTask like so (but I don't think I am doing this the correct way):

private class MyTask extends AsyncTask<Void, Void, List<HashMap<String, String>>> { @Override protected List<HashMap<String, String>> doInBackground(Void... params) { List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map = new HashMap<String, String>(); try { Document doc = Jsoup.connect("http://myurl.com").get(); Elements formalNames = doc.select("li a table tbody tr td span.user_name"); Elements userNames = doc.select("li a"); for (Element formalName : formalNames) { map.put("col_1", formalName.text()); fillMaps.add(map); System.out.println(formalName.text()); } for (Element userName : userNames) { map.put("col_2", userName.attr("href").toString()); fillMaps.add(map); System.out.println(userName.attr("href").toString()); } } catch (IOException e) { e.printStackTrace(); } return fillMaps; } @Override protected void onPostExecute(List<HashMap<String, String>> result) { String[] from = new String[] {"col_1", "col_2"}; int[] to = new int[] { R.id.text1, R.id.text2 }; ListView _listview = (ListView)findViewById(R.id.listView1); SimpleAdapter _adapter = new SimpleAdapter(FriendsActivity.this, fillMaps, R.layout.friends, from, to); _listview.setAdapter(_adapter); } }

This successfully prints out the info I want, but it does not populate the ListView. I am have tried rearranging, etc. but still, no luck. I would be mighty grateful for any help at all.

最满意答案

检查SimpleAdapter类中的getView()。 getView()返回的视图应该正确显示每个项目。

您可以在该过程完成时调用_adapter.notifyDataSetChanged()

更新

好吧,我想我发现你可能引用的例子。 问题是,您一次又一次地使用相同的HashMap。 如果你一次又一次地在任何键(“col_1”或“col_2”)上放置任何字符串,它将只保存最后一个。 因此,当您在屏幕上显示它时(在onPostExecute之后),所有视图将显示最后的formalName和userName,因为列表中添加的所有HashMap仅保存相同的最后一个(它们实际上是同一个HashMap)。

我建议你每次在fillMaps上添加新的HashMap。

check getView() in your SimpleAdapter class. the returned view by getView() should show one each item properly.

You can call _adapter.notifyDataSetChanged() when the process finished

updated

ok i guess i found the example u might referenced. The problem is, you are using the same HashMap again and again. if you put any String on any key("col_1" or "col_2") again and again, it will only save the last one. So when the time you show it on the screen(after onPostExecute), the all views will show the last formalName and userName, because all HashMap added on your list are saving the same last one only(they are actually the one same HashMap).

I suggest you to make new HashMap for each time you add it on the fillMaps.

使用Jsoup填充ListView(Using Jsoup to populate a ListView)

我试图使用Jsoup解析一个网站,获取我得到的信息并填充ListView 。 HTML看起来像这样:

<ul class="list_view"> <li> <a href="/username/" > <table class="pinner"> <tbody> <tr> <td class="first_td"> <img src="http://myimgurl.com/img.jpg"> </td> <td> <span class="user_name">User Name</span> </td> </tr> </tbody> </table> </a> </li> </ul>

因此,从这个HTML中,我需要从a标签和span.user_name文本中获取href 。 我需要同时使用这两个元素并将它们存储在HashMap (我认为??)现在,我在AsyncTask就像这样(但我不认为我这样做是正确的):

private class MyTask extends AsyncTask<Void, Void, List<HashMap<String, String>>> { @Override protected List<HashMap<String, String>> doInBackground(Void... params) { List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map = new HashMap<String, String>(); try { Document doc = Jsoup.connect("http://myurl.com").get(); Elements formalNames = doc.select("li a table tbody tr td span.user_name"); Elements userNames = doc.select("li a"); for (Element formalName : formalNames) { map.put("col_1", formalName.text()); fillMaps.add(map); System.out.println(formalName.text()); } for (Element userName : userNames) { map.put("col_2", userName.attr("href").toString()); fillMaps.add(map); System.out.println(userName.attr("href").toString()); } } catch (IOException e) { e.printStackTrace(); } return fillMaps; } @Override protected void onPostExecute(List<HashMap<String, String>> result) { String[] from = new String[] {"col_1", "col_2"}; int[] to = new int[] { R.id.text1, R.id.text2 }; ListView _listview = (ListView)findViewById(R.id.listView1); SimpleAdapter _adapter = new SimpleAdapter(FriendsActivity.this, fillMaps, R.layout.friends, from, to); _listview.setAdapter(_adapter); } }

这会成功打印出我想要的信息,但它不会填充ListView 。 我已经尝试过重新安排等等,但仍然没有运气。 我会非常感激任何帮助。

I am trying to parse a website using Jsoup, take the info I get and populate a ListView. The HTML looks like this:

<ul class="list_view"> <li> <a href="/username/" > <table class="pinner"> <tbody> <tr> <td class="first_td"> <img src="http://myimgurl.com/img.jpg"> </td> <td> <span class="user_name">User Name</span> </td> </tr> </tbody> </table> </a> </li> </ul>

So, from this HTML, I need to get the href from the a tag, and also the span.user_name text. I need to take both of these elements and store them in a HashMap (I think??) Right now, I have this in an AsyncTask like so (but I don't think I am doing this the correct way):

private class MyTask extends AsyncTask<Void, Void, List<HashMap<String, String>>> { @Override protected List<HashMap<String, String>> doInBackground(Void... params) { List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map = new HashMap<String, String>(); try { Document doc = Jsoup.connect("http://myurl.com").get(); Elements formalNames = doc.select("li a table tbody tr td span.user_name"); Elements userNames = doc.select("li a"); for (Element formalName : formalNames) { map.put("col_1", formalName.text()); fillMaps.add(map); System.out.println(formalName.text()); } for (Element userName : userNames) { map.put("col_2", userName.attr("href").toString()); fillMaps.add(map); System.out.println(userName.attr("href").toString()); } } catch (IOException e) { e.printStackTrace(); } return fillMaps; } @Override protected void onPostExecute(List<HashMap<String, String>> result) { String[] from = new String[] {"col_1", "col_2"}; int[] to = new int[] { R.id.text1, R.id.text2 }; ListView _listview = (ListView)findViewById(R.id.listView1); SimpleAdapter _adapter = new SimpleAdapter(FriendsActivity.this, fillMaps, R.layout.friends, from, to); _listview.setAdapter(_adapter); } }

This successfully prints out the info I want, but it does not populate the ListView. I am have tried rearranging, etc. but still, no luck. I would be mighty grateful for any help at all.

最满意答案

检查SimpleAdapter类中的getView()。 getView()返回的视图应该正确显示每个项目。

您可以在该过程完成时调用_adapter.notifyDataSetChanged()

更新

好吧,我想我发现你可能引用的例子。 问题是,您一次又一次地使用相同的HashMap。 如果你一次又一次地在任何键(“col_1”或“col_2”)上放置任何字符串,它将只保存最后一个。 因此,当您在屏幕上显示它时(在onPostExecute之后),所有视图将显示最后的formalName和userName,因为列表中添加的所有HashMap仅保存相同的最后一个(它们实际上是同一个HashMap)。

我建议你每次在fillMaps上添加新的HashMap。

check getView() in your SimpleAdapter class. the returned view by getView() should show one each item properly.

You can call _adapter.notifyDataSetChanged() when the process finished

updated

ok i guess i found the example u might referenced. The problem is, you are using the same HashMap again and again. if you put any String on any key("col_1" or "col_2") again and again, it will only save the last one. So when the time you show it on the screen(after onPostExecute), the all views will show the last formalName and userName, because all HashMap added on your list are saving the same last one only(they are actually the one same HashMap).

I suggest you to make new HashMap for each time you add it on the fillMaps.