Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to put Element text on one row in ListActivity with this
    primarykey
    data
    text
    <p>I am using this code to retrieve a set of game titles platform and release dates:</p> <pre><code>public class HtmlparserExampleActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayList&lt;String&gt; gameList = new ArrayList&lt;String&gt;(); Document doc = null; try { doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Get all td's that are a child of a row - each game has 4 of these Elements games = doc.select("tr &gt; td.indexList1, tr &gt; td.indexList2"); // Iterator over those elements ListIterator&lt;Element&gt; postIt = games.listIterator(); while (postIt.hasNext()) { // Add the game text to the ArrayList gameList.add(postIt.next().text()); Log.v(TAG, games.text()); } String[] items = new String[gameList.size()]; gameList.toArray(items); this.setListAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, items)); } } </code></pre> <p>It works perfectly like this. The only problem is if you go to the webpage I'm retrieving the items from. It displays the game title, release date, and the platform all on separate list rows. </p> <p>How could I put the title, platform release date and all in one row for EACH game I retrieve?</p> <p>EDIT - ClassCast Exception error:</p> <blockquote> <p>08-15 16:35:12.310: ERROR/AndroidRuntime(21589): FATAL EXCEPTION: main 08-15 16:35:12.310: ERROR/AndroidRuntime(21589): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams 08-15 16:35:12.310: ERROR/AndroidRuntime(21589): at android.widget.ListView.setupChild(ListView.java:1790) 08-15 16:35:12.310: ERROR/AndroidRuntime(21589): at android.widget.ListView.makeAndAddView(ListView.java:1759) 08-15 16:35:12.310: ERROR/AndroidRuntime(21589): at android.widget.ListView.fillDown(ListView.java:656) 08-15 16:35:12.310: ERROR/AndroidRuntime(21589): at android.widget.ListView.fillFromTop(ListView.java:716) 08-15 16:35:12.310: ERROR/AndroidRuntime(21589): at android.widget.ListView.layoutChildren(ListView.java:1609) 08-15 16:35:12.310: ERROR/AndroidRuntime(21589): at android.widget.AbsListView.onLayout(AbsListView.java:1800) 08-15 16:35:12.310: ERROR/AndroidRuntime(21589): at android.view.View.layout(View.java:9581)</p> </blockquote> <p>Here is the code I constructed following the tutorial and your answer:</p> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList&lt;GameRelease&gt; gameList = new ArrayList&lt;GameRelease&gt;(); Document doc = null; try { doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Get all td's that are a child of a row - each game has 4 of these Elements games = doc.select("tr &gt; td.indexList1, tr &gt; td.indexList2"); // Iterator over those elements ListIterator&lt;Element&gt; postIt = games.listIterator(); while (postIt.hasNext()) { // ... while (postIt.hasNext()) { // Add the game text to the ArrayList String name = postIt.next().text(); String platform = postIt.next().text(); String genre = postIt.next().text(); String releaseDate = postIt.next().text(); gameList.add(new GameRelease(name, platform, genre, releaseDate)); Log.v(TAG, games.text()); } this.setListAdapter(new GameReleaseAdapter(this, gameList)); } } private class GameReleaseAdapter extends ArrayAdapter&lt;GameRelease&gt; { private ArrayList&lt;GameRelease&gt; items; public GameReleaseAdapter(Context context, ArrayList&lt;GameRelease&gt; items) { // TODO: make a layout for each item which you'd call (for example) itemLayout super(context, R.layout.item, items); this.items = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO: return an item view styled however you want or as shown in the tutorial View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.row, null); } GameRelease o = items.get(position); TextView tt = (TextView) v.findViewById(R.id.toptext); TextView bt = (TextView) v.findViewById(R.id.bottomtext); tt.setText(o.getName()); bt.setText(o.getReleaseDate()); return bt; } } } </code></pre> <p>EDIT - Layout item.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;/LinearLayout&gt; </code></pre> <p>row.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:padding="6dip"&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="fill_parent"&gt; &lt;TextView android:id="@+id/toptext" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center_vertical" /&gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:id="@+id/bottomtext" android:singleLine="true" android:ellipsize="marquee" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload