我正在使用它來獲取網頁中的元素列表。 http://www.gamespy.com/index/release.html如何加載這些元素來在android中填充列表?
// 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頁面源代碼。
編輯 - 讓我空指針錯誤
/** 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>
我在給定的網頁中看不到'div.FP_Up_TextWrap'。 – BalusC
您的權利來自我的舊項目。你能把它應該在答案中嗎? –