Reputation: 31
I am stuck in a problem and I hope you can give me suggestions on how to improve my code. I was coding about displaying images from URLs into listview, yet it does not appear. Here is my code:
public class MainActivity extends ListActivity {
private String[] imgNames;
private String[] imgUrls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
String[] imgNames = {"ToDo","Gmail","LinkedIn","Facebook","Google Play"};
this.imgNames = imgNames;
String[] imgUrls = {
"http://ww1.prweb.com/prfiles/2010/05/11/1751474/gI_TodoforiPadAppIcon512.png.jpg",
"http://cdn4.iosnoops.com/wp-content/uploads/2011/08/Icon-Gmail_large-250x250.png",
"http://kelpbeds.files.wordpress.com/2012/02/lens17430451_1294953222linkedin-icon.jpg?w=450",
"http://snapknot.com/blog/wp-content/uploads/2010/03/facebook-icon-copy.jpg",
"https://lh3.googleusercontent.com/-ycDGy_fZVZc/AAAAAAAAAAI/AAAAAAAAAAc/Q0MmjxPCOzk/s250-c-k/photo.jpg"
};
this.imgUrls = imgUrls;
ListAdapter adapter = new ListAdapter(this, imgNames, imgUrls);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
public class ListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] imgUrls;
private final String[] imgNames;
public ListAdapter(Context context, String[] imgNames, String[] imgUrls) {
super(context, R.layout.activity_main, imgNames);
this.imgNames = imgNames;
this.imgUrls = imgUrls;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
View row = (View)inflater.inflate(R.layout.activity_main, parent, false);
TextView name = (TextView)row.findViewById(R.id.TextView);
new DownloadImageTask((ImageView) findViewById(R.id.ImageView)).execute(imgUrls[position]);
name.setText(imgNames[position]);
return row;
}
}
}
Do you have any suggestions on how to improve my code. I tried using this code without list views and it worked but unfortunately it does not work on listviews.
Upvotes: 3
Views: 10049
Reputation: 3349
Don't use an async task to do this. Use a well tested imageloader library.
I recommend https://github.com/koush/UrlImageViewHelper
I have used this library in three high profile apps, and it is working wonderfully for half a million users.
Then in your getView method do this
UrlImageViewHelper.setUrlDrawable((ImageView) findViewById(R.id.ImageView), imgUrls[position]);
Upvotes: 14