如何加载这些元素来填充 android 中的列表?
我正在使用它从网页获取元素列表。 http://www.gamespy.com/index/release.html
// Get all the game elements
Elements games = doc.select("div.FP_Up_TextWrap b");
// Create new ArrayList
ArrayList<String> gameList = new ArrayList<String>();
// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}
这将返回所有标签是我想要的。但它返回带有标题的完整标签,我只想要标题、发布日期和 Img sr。 我想将其返回到列表视图。 我该怎么做呢?我可能做得完全错误,所以你们可能想查看 HTML 页面源代码。
编辑 - 给我 NullPointer 错误
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputTextView = (TextView)findViewById(R.id.outputTextView);
ListView list = (ListView)findViewById(R.id.list);
ArrayList<String> gameList = new ArrayList<String>();
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 > td.indexList1, tr > td.indexList2");
// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}
String[] items = new String[gameList.size()];
gameList.toArray(items);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
// error points here
list.setAdapter(adapter);
}
}
编辑 - 列表的布局
<?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>
I am using this to get a list of elements from a webpage.
http://www.gamespy.com/index/release.html
// Get all the game elements
Elements games = doc.select("div.FP_Up_TextWrap b");
// Create new ArrayList
ArrayList<String> gameList = new ArrayList<String>();
// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}
This returns all the tags with which is what I want. But it returns the full tag with the title I just want the title the release date and a Img sr.
I would like to return it to a list view.
How would I go about doing this? I maybe doing it totally wrong so you guys may want to check out the HTML page source.
EDIT - Gives me NullPointer error
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputTextView = (TextView)findViewById(R.id.outputTextView);
ListView list = (ListView)findViewById(R.id.list);
ArrayList<String> gameList = new ArrayList<String>();
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 > td.indexList1, tr > td.indexList2");
// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}
String[] items = new String[gameList.size()];
gameList.toArray(items);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
// error points here
list.setAdapter(adapter);
}
}
EDIT - Layout for the list
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将无法使用此代码“返回列表视图”,但您可以创建自己的列表视图子类,然后可以按照相同的技术返回该子类:
最终归结为设置列表适配器名单。如果您需要创建自己的列表适配器,您可以子类化并实现 ListAdapter 来数据绑定其他对象类型,但已经有一个预定义的适配器 (ArrayAdapter) 用于处理 ListView/ListActivity 数据绑定的简单字符串集。
更新 - 发帖者请求额外的实现细节:
给定:
您可以创建适配器:
我这里没有 Java 编译器,所以如果这不只是复制/粘贴到您的项目中,但它应该,请不要打扰我远高于 90%。
快乐编码。
乙
You won't be able to "return a list view" using this code, but you could create your own list view sub class which you could then return, following the same techniques:
Ultimately what it comes down to is setting the list adapter for the list. If you need to create your own list adapter you can subclass and implement ListAdapter to databind other object types, but there's already a predefined adapter (ArrayAdapter) for handling simple string sets for ListView/ListActivity databinding.
UPDATE - Poster requested additional implementation details:
Given:
You might create the adapter:
I don't have a Java compiler here, so please don't ding me if this doesn't just copy/paste right into your project, but it should be well north of 90% there.
Happy coding.
B
这应该收集标题和发布日期。您还想要哪张图片?
This should gather the titles and release dates. Which image did you also want?