listview显示来自互联网的图像和来自sqlite的数据

时间:2012-01-20 08:22:39

标签: android listview

我的光标从sqlite获取3列数据,第1列是食谱名称,第2列是食谱作者,第3列是食谱图像的URL。这是我的代码的一部分

private static final String fields[] = { "recipeID", "recipeName", "thumbnail"};
Cursor mCursor = dbAdapter.getAllTitles(); //get all titles returns the recipe name, author and thumbs
startManagingCursor(mCursor);
dataSource = new SimpleCursorAdapter(this, R.layout.recipelist, mCursor, fields,    
                  new int[] { R.id.recipeAuthor, R.id.recipeName, R.id.recipeThumb });
setListAdapter(dataSource);

显然,上面的代码不是显示配方图像,而是在R.id.recipeThumb上显示配方图像的URL。如何让它显示来自互联网的图片?

2 个答案:

答案 0 :(得分:0)

本讨论中答案下方的帖子可能对您有用:

Lazy load of images in ListView

答案 1 :(得分:0)

您需要一个CustomAdapter并覆盖getView()方法。 在基于Url的getView()方法中,您可以创建一个ImageView。

getView(...) {
   //First Create a Drawable from the InputStream of the Url 
   //store this drawable locally so that you don't have to fetch it again 
   Drawable imgDrawable = Drawable.createFromStream(inputStream,"");
   //set this in the imageView
   imgView.setImageDrawable(imgDrawable);

   //set this imgView in the contentview and return

}