Displaying images from URL in Listview

I am stuck with an issue and hopefully you can give me some advice on how to improve my code. I have encoded displaying images from urls in listview but it is not showing. 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 for improving the code. I tried using this code without looking at lists, but it worked, but unfortunately it doesn't work in lists.

+3


source to share


1 answer


Don't use an async task for this. Use the well tested imageloader library.

I recommend https://github.com/koush/UrlImageViewHelper

I've used this library in three high profile applications and it works great for half a million users.



Then in your getView method do this

UrlImageViewHelper.setUrlDrawable((ImageView) findViewById(R.id.ImageView), imgUrls[position]);

      

+14


source







All Articles