2011-08-16 69 views
0

我正在使用它來檢索項目列表並將它們放入列表中。每個項目也檢索URL。我使用ListActivity如何使用此設置onItemClick偵聽器

// 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"); 
     Elements links = doc.getElementsByTag("a"); // or getElementsByClass("b1"); 


     // Iterator over those elements  
     ListIterator<Element> postIt = games.listIterator();   
     while (postIt.hasNext()) {  
      // ...It 

      while (postIt.hasNext()) {  
       // Add the game text to the ArrayList  
       Element name = postIt.next(); 
       String nameString = name.text(); 
       String platform = postIt.next().text(); 
       Element url = name.select("a").first(); 

      //Here i retreive the url string for each item 
         String urlString = url.attr("href"); 

       String genre = postIt.next().text(); 
       String releaseDate = postIt.next().text(); 
       gameList.add(new GameRelease(nameString, platform, genre, releaseDate, urlString)); 
       Log.v(TAG, urlString); 
      } 

      this.setListAdapter(new GameReleaseAdapter(this, gameList)); 
     }   

我創建了自己ArrayAdapter這裏...

private class GameReleaseAdapter extends ArrayAdapter<GameRelease> { 

    private ArrayList<GameRelease> items; 

    public GameReleaseAdapter(Context context, ArrayList<GameRelease> 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); 
     TextView plat = (TextView)v.findViewById(R.id.platform); 
     TextView genre = (TextView)v.findViewById(R.id.genre); 

     tt.setText(o.getName()); 
     bt.setText("RELEASE DATE: " +o.getReleaseDate()); 
     plat.setText("PLATFORM: " + o.getPlatform()); 
     genre.setText("GENRE: " + o.getGenre()); 

     return v; 

} 


} 

}

我怎麼能存儲的URL,所以當項目被點擊的URL是每個項目額外發送給另一項活動?我如何設置每個項目到自己的網址?我如何讓android知道列表中的這個項目被點擊了,它應該是這個特定項目的URL?

編輯 - 像這樣嗎?它給了我噸的語法錯誤

  while (postIt.hasNext()) {  
       // Add the game text to the ArrayList  
       Element name = postIt.next(); 
       String nameString = name.text(); 
       String platform = postIt.next().text(); 
       Element url = name.select("a").first(); 
       String urlString = url.attr("href"); 

       String genre = postIt.next().text(); 
       String releaseDate = postIt.next().text(); 
       gameList.add(new GameRelease(nameString, platform, genre, releaseDate, urlString)); 
       Log.v(TAG, urlString); 
      } 

      this.setListAdapter(new GameReleaseAdapter(this, gameList)); 
      final GameReleaseAdapter myGameReleaseAdapter =new GameReleaseAdapter(this,gameList); 

      setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        GameRelease selectedGameRelease =myGameReleaseAdapter .getItem(position); 
        String urlString=selectedGameRelease.getUrlString(); 
        //Make what you like with the url 

       } 
      }); 



     }   

回答

1
當您創建您的 ActivityGameReleaseAdapter

,請確保您有onCreate

1-閱讀您的gameList

對它的引用

2-閱讀完後,請執行以下操作:

final GameReleaseAdapter myGameReleaseAdapter =new GameReleaseAdapter(this,gameList); 

3-

getListView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       GameRelease selectedGameRelease =myGameReleaseAdapter .getItem(position); 
       String urlString=selectedGameRelease.getUrlString(); 
       //Make what you like with the url 

      } 
     }); 

OnItemClickListener可以AdapterView.OnItemClickListener

+0

我在哪裏可以把這個代碼在這裏找到? –

+0

把它放在你創建你的'ListView'的地方! –

+0

在GameReleaseAdapter內部還是在onCreate之外? –

相關問題