问题
In the way to create a Listview with some images I decided to do an AsyncTask for getting my image. So my code is like this:
private class StableArrayAdapter extends ArrayAdapter<Request>{
private Context context;
private Bitmap tempobmp;
final User myUser = myapp.getUser();
private List<Request> mylist;
public StableArrayAdapter(Context context,List<Request> mylist){
super(context, R.layout.request_layout, mylist);
this.context = context;
this.mylist = mylist;
}public class ViewHolder{
public ImageView image;
}
public View getView(int position,View convertView,ViewGroup parent){
View vi = convertView;
if(vi==null){
final Integer positionInt = position;
final ViewHolder holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.request_layout,parent,false);vi.findViewById(R.id.totalLisV);
holder.image = (ImageView) vi.findViewById(R.id.requestImage);
vi.setTag(holder);
}
final ViewHolder holder = (ViewHolder) vi.getTag();
final Request s = mylist.get(position);
final User tempoUser = new User(s.getAsking_user().getUsername());
tempoUser.fetch(new StackMobModelCallback(){
@Override
public void failure(StackMobException e) {
// the call failed
}
@Override
public void success() {
// TODO Auto-generated method stub
}
});
new GetImageData().execute(tempoUser,vi);
return vi;
}
In the way to get this image the magic has to happen in the AsyncTask which is the folowing (For the moment I am using an hardcoded url for testing purpose):
private class GetImageData extends AsyncTask<Object, Void, Bitmap> {
private View myView;
private Bitmap bmp = null;
@Override
protected Bitmap doInBackground(Object... users) {
User user =(User) users[0];
myView = (View)users[1];
Bitmap bmp = null ;
try{
String urlString = "http://digital-photography-school.com/wp-content/uploads/2013/03/Acorn256.png";
URL url = new URL(urlString);
InputStream is = (InputStream) url.getContent();
bmp = BitmapFactory.decodeStream(is);
}
catch(Exception e){
Log.d("asykException","asykE"+e);
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
ImageView albumArt = (ImageView) myView.findViewById(R.id.requestImage);
albumArt.setImageBitmap(bmp);
}
}
Here I come to a very strange issue. Indeed In my listview the default image is replace by...nothing. the imageView square remains transparent.Nontheles if I check in the log the bitmap state just before the return this one is not empty. My queston here is: Why do I get a transparent picture when the Bitmap that I get in my AsyncTask is not null?
NB: this issue is happening, no matter what I put in the URL.
Thank you guys :)
回答1:
Don't call the AsyncTask in your Adapter class. I don't think this is good practice plus the getView() is probably returning before your task has finished. So, instead call the task from your Activity then in your onPostExecute() initialize your Adapter and set it on your ListView there. Or if you have already initialized your Adapter then you can call notifyDataSetChanged() on your Adapter in onPostExecute().
回答2:
You want to get the image from Internet, use this code :
URL url = new URL(imgurl);
URLConnection con = url.openConnection();
con.connect();
InputStream is = con.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
And now you get the Bitmap object, just show on ImageView. That's all.
回答3:
no you need to using notifyDatasetChange so the adapter will add new contents to your listview see more details here Is ArrayAdapter thread safe in android? If not, what can I do to make it thread safe?
but this approach doesn't help you very well you need to make your listview more faster , so here some tips to how to append images to listview in faster way in section 3) Loading images of this article
http://optimizationtricks.blogspot.in/?utm_source=Android+Weekly&utm_campaign=d201f012d0-Android_Weekly_85&utm_medium=email&utm_term=0_4eb677ad19-d201f012d0-337298357
and here the Lib will help you to download image on listview in more faster way https://github.com/nostra13/Android-Universal-Image-Loader
hope this help you
来源:https://stackoverflow.com/questions/21246622/getting-image-from-an-url-the-view-stays-empty