使用布局充气机延迟加载图像
我是 android 新手,所以在
我想做的事情上寻求一些帮助:
我正在下载一个 xml 文件,将其加载到一个对象中,并使用布局充气器显示它。
在 xml 中有一个图像链接,然后我尝试启动一个单独的异步任务来延迟加载给定链接中的图像。 xml下载工作正常,它只是我正在努力解决的图像位。我想我已经快到了。任何帮助将非常欢迎
我的代码:
class MyRowAdapter extends ArrayAdapter<XMLItem> {
Activity context;
MyRowAdapter (Activity context) {
super(context, R.layout.news_row, items);
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=context.getLayoutInflater();
row = inflater.inflate(R.layout.news_row, null);
}
XMLItem _item = items.get(position);
rowpos = position;
Log.v(TAG,""+position);
TextView headline = (TextView)row.findViewById(R.id.Headline);
headline.setText(_item.getHeadline());
TextView imagepath = (TextView)row.findViewById(R.id.Imagepath);
imagepath.setText(_item.getImagepath());
try {
ImageView img = (ImageView) row.findViewById(R.id.storyimage);
img.setImageBitmap(_item.getBitmap());
} catch(IndexOutOfBoundsException e) {
new DownloadImageTask().execute(_item.getImagepath());
}
return row;
}
}
private class DownloadImageTask extends AsyncTask<String,Integer,Bitmap>{
private ImageView img;
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap bmImg = null;
try {
URL imageurl = new URL(urls[0]);
Log.v("imageurl","="+imageurl);
HttpURLConnection conn= (HttpURLConnection)imageurl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//HttpURLConnection conn =(HttpURLConnection)imageurl.openConnection();
}
return bmImg;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
XMLItem _item = items.get(rowpos);
super.onPostExecute(result);
img.setImageBitmap(result);
_item.setBitmap(result);
Log.v("image task","onpostexecute");
myRowAdapter.notifyDataSetChanged();
}
}
i am new to android, so seeking some help on this
what i am trying to do:
i am downloading an xml file, loading it into an object, and displaying it using a layout inflator.
within the xml there is a link to an image, i am then trying to launch a seperate asynctask to lazy load the images from the link given. the xml download works fine, its just the images bit i am struggling on. i think i am nearly there. any help would be greatly welcome
my code:
class MyRowAdapter extends ArrayAdapter<XMLItem> {
Activity context;
MyRowAdapter (Activity context) {
super(context, R.layout.news_row, items);
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=context.getLayoutInflater();
row = inflater.inflate(R.layout.news_row, null);
}
XMLItem _item = items.get(position);
rowpos = position;
Log.v(TAG,""+position);
TextView headline = (TextView)row.findViewById(R.id.Headline);
headline.setText(_item.getHeadline());
TextView imagepath = (TextView)row.findViewById(R.id.Imagepath);
imagepath.setText(_item.getImagepath());
try {
ImageView img = (ImageView) row.findViewById(R.id.storyimage);
img.setImageBitmap(_item.getBitmap());
} catch(IndexOutOfBoundsException e) {
new DownloadImageTask().execute(_item.getImagepath());
}
return row;
}
}
private class DownloadImageTask extends AsyncTask<String,Integer,Bitmap>{
private ImageView img;
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap bmImg = null;
try {
URL imageurl = new URL(urls[0]);
Log.v("imageurl","="+imageurl);
HttpURLConnection conn= (HttpURLConnection)imageurl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//HttpURLConnection conn =(HttpURLConnection)imageurl.openConnection();
}
return bmImg;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
XMLItem _item = items.get(rowpos);
super.onPostExecute(result);
img.setImageBitmap(result);
_item.setBitmap(result);
Log.v("image task","onpostexecute");
myRowAdapter.notifyDataSetChanged();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您实现: Android - 如何执行延迟加载,而不是实现从指定 URL 下载图像的单独任务ListView 中的图像
Instead of implementing the separate task for downloading image from the specified URL, i would suggest you to implement: Android - How do I do a lazy load of images in ListView
我没有看到 DownloadImageTask 中初始化 img 变量的地方。可能您忘记将其传递给构造函数:
但通常我建议使用 UniversalImageLoader 用于延迟图像加载和显示。如果需要,它可以将您的图像缓存在内存和/或文件系统(SD 卡)中。
简单的使用示例:
它将真正简化您的生活。
I don't see the place where img variable is initialized in DownloadImageTask. May be you forgot pass it to constructor:
But generally I recommend use UniversalImageLoader for lazy image loading and displaying. It can cache your images in memory and/or on file system (SD card) if you need.
Simple usage example:
It will really simplify your life.