Reputation: 15
I'm building a personal app that, using JSOUP, puts the text of certain links into a ListView. For each link there should be a corresponding entry in ListView with the text from that link. When I run the app it successfully parses all of these texts. However, it places all of the text clumped up together in a single ListView entry then does the exact same thing over for consecutive entry. Where am I going wrong? Relevant code below:
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);
}
}
}
A visual representation of what I'm currently getting:
vs. what I want to be getting:
Many thanks!
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>
Upvotes: 0
Views: 1747
Reputation: 527
Change
for (Element beers : beer) {
beerList.add(beer.text());
}
to
for (Element beers : beer) {
beerList.add(beers.text()+"\n");
}
u miss s in the beer
Try this
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);
}
}
}
Upvotes: 1