从列表视图适配器中加载URL中的图像

问题描述:

我正在收集youtube的gdata API中的数据并希望使用视频的缩略图。我已经有了每个缩略图的网址,但是我无法让图像显示在我的列表视图中。位图当前不返回null,但是我无法用我当前的实现(我知道的?)调用imageview.setImageBitmap(bitmap)方法。请帮助,如果你知道更好的方式来做到这一点!由于从列表视图适配器中加载URL中的图像

public class ResultListActivity extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    String searchquery = null; 
    Intent starter = getIntent(); 
    Bundle extras = starter.getExtras(); 
    if (extras != null) { 
     searchquery = extras.getString("search"); 
    }   
    final String URL = "http://gdata.youtube.com/feeds/api/videos?max-results=5&alt=json&author=user&q=" + searchquery; 
    final ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(); 
    final QueryYoutube query;  

    query = new QueryYoutube(); 
    boolean success = query.searchYoutube(URL); 

    if (success == false) { 
     Toast.makeText(this, "Sorry, no matching results found.", Toast.LENGTH_SHORT).show(); 
     Intent intent = new Intent(ResultListActivity.this, YoutubeActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
     startActivity(intent); 
     finish(); 
    } 

    int size = query.getNumberResults(); 
    HashMap<String, Object> row = new HashMap<String, Object>(); 

    for (int i=0; i<size; i++) { 
     Bitmap bitmap = null; 
     Drawable d = null;   
     try {     
      bitmap = BitmapFactory.decodeStream((InputStream)new URL(query.getThumbnail(i)).getContent()); 
      bitmap.setDensity(Bitmap.DENSITY_NONE); 
      d = new BitmapDrawable(bitmap); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     row = new HashMap<String, Object>(); 
     row.put("Thumbnail", d);    
     row.put("Title", query.getTitle(i)); 
     row.put("Description", query.getDescription(i)); 
     data.add(row); 
    } 

    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.row, 
       new String[] {"Thumbnail","Title","Description"}, 
       new int[] {R.id.thumb, R.id.title, R.id.description}); 
    setListAdapter(adapter); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent lVideoIntent = new Intent(null, Uri.parse("ytv://"+query.getVideoID(position)), 
        ResultListActivity.this, OpenYouTubePlayerActivity.class); 
      startActivity(lVideoIntent);    
     } 
    }); 

} 
} 

我建议的方式提供的URI的图像这样做是因为你应该扩展ArrayAdapter类,然后在getView方法中你发起一个AsyncTask t o在UI线程之外的线程中下载缩略图,然后将位图设置为onPostExecute中的imageview。

你应该使用类似HttpUrlConnection的东西来得到InputStream的图像解码成Bitmap。如果您不确定Bitmap的尺寸,在实际创建Bitmap对象之前检查图像的尺寸非常重要,因为它会导致OutOfMemoryError因此会事先设置为BitmapFactory.OptionsinJustDecodeBoundstrue然后获取图像的尺寸,然后选择一个尺寸足够的inSampleSize并再次解码以检索不太大的Bitmap

您还应该拥有某种Bitmap缓存,以便您不必始终从网上检索。

从文档的SimpleAdapter

ImageView. The expected bind value is a resource id or a string and setViewImage(ImageView, int) or setViewImage(ImageView, String) is invoked. 

你应该为缩略图领域,而不是Bitmap小号

Uri uri = Uri.parse(query.getThumbnail(i)).getContent()); 
+0

我得到了的getContent()方法是未定义类型乌里 – Greg 2012-03-23 14:51:38

您至少有两种方法:
1)您可以使用getView方法,缓存和异步加载thubnails实现自己的列表适配器;
2)您必须执行SimpleAdapter.ViewBinder接口。 事情是这样的:

private static class GrekViewBinder implements SimpleAdapter.ViewBinder 
{ 
    public boolean setViewValue(View view, Object data, String textRepresentation) 
    { 
     if (view.getId() == R.id.thumb) 
     { 
      ((ImageView)view).setImageXXX(... use `data` argument...); 
      return true; 
     } 
     return false; 
    }  
} 

并结合本活页夹到你的适配器:

adapter.setViewBinder(new GrekViewBinder());