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