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

时间:2012-03-22 21:57:01

标签: android http bitmap android-listview drawable

我正在从youtube的gdata API收集数据,并希望使用该视频的缩略图。我已经有每个缩略图的网址,但是我无法将图像显示在我的列表视图中。位图当前没有返回null,但是我无法用我当前的实现调用imageview.setImageBitmap(bitmap)方法(我知道吗?)。如果您知道更好的方法,请帮忙!感谢

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

}
}

3 个答案:

答案 0 :(得分:1)

我建议这样做的方法是你应该扩展ArrayAdapter类,然后在你发起getView的{​​{1}}方法中,在UI之外的线程中下载缩略图线程,然后将位图设置为AsyncTask中的imageview。

您应该使用onPostExecute之类的内容将图片HttpUrlConnection解码为InputStream。如果您不确定Bitmap的尺寸,在实际创建导致Bitmap的{​​{1}}对象之前检查图像的大小非常重要,请事先设置{{1} } BitmapOutOfMemoryError,然后获取图片的尺寸,然后选择足够的BitmapFactory.Options并再次解码以检索不太大的inJustDecodeBounds

您还应该拥有某种true缓存,这样您就不必始终从网上检索。

答案 1 :(得分:0)

来自SimpleAdapter的文档:

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

您应该将RSS作为缩略图字段提供给图像,而不是Bitmap s

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

答案 2 :(得分:0)

你至少有两种方式:
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;
    }    
}

并将此Binder绑定到您的适配器:

adapter.setViewBinder(new GrekViewBinder());