2011-08-15 73 views
1

我正在使用它來獲取網頁中的元素列表。 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> 
+0

我在給定的網頁中看不到'div.FP_Up_TextWrap'。 – BalusC

+0

您的權利來自我的舊項目。你能把它應該在答案中嗎? –

回答

1

你將不能夠「返回一個列表視圖」使用此代碼,但你可以創建您自己的列表視圖子類,然後您可以按照相同的技巧返回:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ListView listView1 = (ListView) findViewById(R.id.listView1); 

    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());  
    }   

    String[] items = new String[gameList.size()]; 
    gameList.toArray(items); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.some_list_item_view, items); 

    listView1.setAdapter(adapter); 
} 

Ultimate最後歸結爲設置列表的適配器。如果您需要創建自己的列表適配器,您可以繼承並實現ListAdapter來綁定其他對象類型,但已經有一個預定義的適配器(ArrayAdapter)用於處理ListView/ListActivity數據綁定的簡單字符串集。

更新 - 海報請求了額外的實現細節:

考慮:

public class GameMeta { 
    private String m_title; 
    private java.util.Date m_releaseDate; 

    public GameMeta() { 
    } 

    public String getTitle(){ 
     return m_title; 
    } 

    public void setTitle(String value) { 
     m_title = value; 
    } 

    public java.util.Date getReleaseDate(){ 
     return m_releaseDate; 
    } 

    public void setReleaseDate(java.util.Date releaseDate){ 
     m_releaseDate = releaseDate; 
    } 
} 

你可以創建適配器:

public class GamesMetaAdapter implements Adapter extends ListAdapter { 
    private ArrayList<GameMeta> m_list = new ArrayList<GameMeta>(); 
    private Context m_context = null; 

    public GamesMetaAdapter(Context context) { 
     m_context = context; 
    } 

    @Override 
    public int getCount(){ 
      return m_list.size(); 
    } 

    @Override 
    public Object getItem(int position){ 
     if(position < m_list.size()){ 
      return m_list.getAt(position); 
     } 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return (long)position; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return IGNORE_ITEM_VIEW_TYPE; 
    } 

    @Override 
    public View getView (int position, View convertView, ViewGroup parent) { 
      TextView simpleView = new TextView(this.m_context); 
      String viewText = this.m_items[position].getTitle() + ", Released " + this.m_items[position].getReleaseDate().toString(); 

      simpleView.setText(this.m_items[position].getTitle()); 

      if(TextView.class.isInstance(convertView)) { 
        convertView = simpleView; 
      } 

      return simpleView; 
    } 


    @Override 
    public int getViewTypeCount() { 
     return 1; 
    } 

    @Override 
    public boolean hasStableIds(){ 
     return false; 
    } 

    @Override 
    public boolean isEmpty() { 
     return m_list.size() == 0; 
    } 

    @Override 
    public void registerDataSetObserver (DataSetObserver observer) { 

    } 

    @Override 
    public void unregisterDataSetObserver (DataSetObserver observer) { 

    } 

    /* LIST ADAPTER MEMBERS */ 
    @Override 
    public boolean areAllItemsEnabled() { 
      return true; 
    } 

    @Override 
    public boolean isEnabled(int position) { 
     return true; 
    }  
} 

我沒有一個Java編譯器在這裏,所以請如果這不僅僅是複製/粘貼到你的項目中,那麼不要嘲笑我,但它應該在90%以上。

快樂編碼。

B

+0

你可以把我需要用來獲得遊戲標題和發佈日期的技術嗎?對於每個人都在元素遊戲?上面的代碼與我的舊項目之一的網頁不匹配 –

+0

我是否在一個課程中完成所有這一切?或者讓單獨的課程?所有這些如何結合在一起? –

+0

如果您要重用該適配器,您可以將該適配器放在一個類中,或者您可以將它變爲ListView類的私有子類。完全針對代碼樣式/標準以及您預期適配器/視圖的任何重用。 – Brian

1

這應該收集標題和發佈日期。你還想要哪張圖?

// 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 release = gameIt.next(); 
    System.out.println(release.text() + "\[email protected]@@@@@"); 
} 
+0

我如何在anroid的列表中返回這個? –

+0

對不起,我沒有任何有關android平臺的經驗。希望別人能夠就此事發表意見。你從另一篇文章看過[這個鏈接](http://appfulcrum.com/?p=234)嗎? –