JSOUP和ListView

时间:2013-12-19 01:42:58

标签: listview android-listview jsoup

我正在构建一个个人应用程序,使用JSOUP将某些链接的文本放入ListView。对于每个链接,ListView中应该有一个相应的条目,其中包含该链接的文本。当我运行应用程序时,它成功解析了所有这些文本。但是,它将所有文本放在一起放在一个ListView条目中,然后对连续输入执行完全相同的操作。我哪里错了?相关代码如下:

public class MainActivity extends Activity {

public Elements beer;
public ArrayList<String> beerList = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.listView1);
    new NewThread().execute();
    adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.beer_name, beerList);
}

public class NewThread extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg) {

        Document doc;
        try {
            doc = Jsoup.connect("URLURLURLURL").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();

            beer = doc.select("a[href*=URL.com/URL/]"); 
            beerList.clear();
            for (Element beers : beer) {
                beerList.add(beer.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        lv.setAdapter(adapter);
    }
}
    }

我目前得到的内容的直观表示:

  • Item1 Item2 Item3 Item4 Item5
  • Item1 Item2 Item3 Item4 Item5
  • Item1 Item2 Item3 Item4 Item5
  • Item1 Item2 Item3 Item4 Item5
  • Item1 Item2 Item3 Item4 Item5

VS。我想得到什么:

  • 第1项
  • 第2项
  • 第3项
  • 第4项
  • 第5项

非常感谢!

activity_main.xml:
         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

simple_list_item_1.xml:

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

    <TextView android:id="@+id/beer_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>    

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

更改

for (Element beers : beer) {
                    beerList.add(beer.text());
                }

for (Element beers : beer) {
                    beerList.add(beers.text()+"\n");
                }

你想念啤酒中的 s

试试这个

  public class MainActivity extends Activity {

    public Elements beer;
    public ArrayList<String> beerList = new ArrayList<String>();
    private ArrayAdapter<String> adapter;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.listView1);
    new NewThread().execute();
    adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.beer_name, beerList);
}

public class NewThread extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg) {

        Document doc;
        try {
            doc = Jsoup.connect("URLURLURLURL").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();

            beer = doc.select("a[href*=URL.com/URL/]"); 
            beerList.clear();
            for (Element beers : beer) {
                beerList.add(beers.text()+"\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        lv.setAdapter(adapter);
    }
}
    }