2011-10-05 46 views
0

我該如何去將這段代碼插入到Android的ListView中?如何將字符串返回到自定義ListView?

List<YouTubeVideo> videos = ym.retrieveVideos(textQuery, maxResults, filter, timeout); 

     for (YouTubeVideo youtubeVideo : videos) { 
      System.out.println(youtubeVideo.getWebPlayerUrl()); 
      System.out.println("Thumbnails"); 
      for (String thumbnail : youtubeVideo.getThumbnails()) { 
       System.out.println("\t" + thumbnail); 
      } 
      System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl()); 
      System.out.println("************************************");   } 
     } 

} 

我會創建一個自定義適配器並使用getView()嗎?如果是這樣,它會看起來如何?

回答

1

是的,我會創建一個自定義適配器並使用getView()。下面是一個帶有TextView和ImageView的行佈局示例代碼:

private static class MyListAdapter extends BaseAdapter 
{ 
    private Context   context; 
    private ArrayList<Item> items; 
    private LayoutInflater mInflater; 

    public MyListAdapter(Context context, ArrayList<Item> items) 
    { 
     this.context = context; 
    this.items = items; 
    mInflater = LayoutInflater.from(context); 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return items.get(position); 
    } 

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

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.row_layout, parent, false);         
     } 

     Item item = items.get(position); 

     TextView tv = (TextView) convertView.findViewById(R.id.data_item); 
     tv.setText(item.getItem()); 

     ImageView iv = (ImageView) convertView.findViewById(R.id.imageView1); 
     iv.setImageBitmap(item.getBitmap()); 

     return convertView; 
    } 
+0

那麼它會如何看起來與我上面的代碼?它會是BaseAdapter或ArrayAdapter? – yoshi24

+0

上面的代碼可能會成爲一種方法,用於在我的代碼中填充ArrayList - ArrayList 。你可以在onCreate方法中做到這一點。我將從這開始着手進行實驗,然後在List數據結構更新時,在後臺獲取數據時可以調用notifyDataSetChanged。我一直在我的代碼中使用BaseAdapter,但你可以切換它的ArrayAdapter和代碼是類似的。 – ciscogambo