开发者

How to put Elements in ListView in android?

I am using this code to retreive Elements from a html page.

// Get all td's that are a child of a row - each game has 4 of these
Elements games = doc.select("tr > td.indexList1, tr > td.indexList2");

// Iterator over those elements
ListIterator<Element> gameIt = games.listIterator();

while (gameIt.hasNext()) {
// Get the title of the game
Element title = gameIt.next();

System.out.println(title.text());

// Unneeded elements
Element platform = gameIt.next();
Element genre = gameIt.next();

// Get the release date of the game
Element rel开发者_Python百科ease = gameIt.next();
System.out.println(release.text() + "\n@@@@@@");
}

How would i go about putting these elements in a ListView with two textviews for the title element and the other for the release element?

EDIT-gives me this error

08-15 14:49:37.730: ERROR/AndroidRuntime(18945): FATAL EXCEPTION: main
08-15 14:49:37.730: ERROR/AndroidRuntime(18945): android.content.res.Resources$NotFoundException: Resource ID #0x7f050001
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.content.res.Resources.getValue(Resources.java:1014)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at    android.content.res.Resources.loadXmlResourceParser(Resources.java:2049)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.content.res.Resources.getLayout(Resources.java:853)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.view.LayoutInflater.inflate(LayoutInflater.java:389)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:375)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:366)
 08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.widget.AbsListView.obtainView(AbsListView.java:1970)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1228)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.widget.ListView.onMeasure(ListView.java:1139)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.view.View.measure(View.java:10828)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
08-15 14:49:37.730: ERROR/AndroidRuntime(18945):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)

Here is my xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/outputTextView"
/>
<ListView    android:layout_height="wrap_content" android:id="@+id/list"   android:layout_width="match_parent"></ListView>
</LinearLayout>


Something like this with the following conditions:

  1. You are using a ListActivity
  2. You create a POJO for a Game.

Code:

List<Game> games = new ArrayList<Game>();
while (gameIt.hasNext()) {

  // Get the title of the game
  Element title = gameIt.next();

  // Unneeded elements
  Element platform = gameIt.next();
  Element genre = gameIt.next();

  // Get the release date of the game
  Element release = gameIt.next();

  Game game = new Game(title, platform, genre, release);
  games.add(game);
}

ArrayAdapter adapter = new ArrayAdapter<Game>(this, R.id.YOUR_LIST_ITEM_VIEW, games);
setListAdapter(adapter);


A ListView is abstracted away from the items themselves and therefore refers to a ListAdapter for the actual items.

So you have to put the actual items in a ListAdapter and then call ListView.setAdapter(ListAdapter yourListAdapter).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜