我是android新手,所以寻求一些帮助
我想做的是:
我正在下载一个 xml 文件,将其加载到一个对象中,并使用布局充气器显示它。
在 xml 中有一个指向图像的链接,然后我尝试启动一个单独的 asynctask 以从给定的链接中延迟加载图像。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();
}
}