android - 在 ListView 中显示来自 URL 的图像

标签 android android-listview android-imageview android-image

我遇到了一个问题,我希望你能给我一些建议,告诉我如何改进我的代码。我正在编码将 URL 中的图像显示到 ListView 中,但它没有出现。这是我的代码:

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

您对如何改进我的代码有什么建议吗?我尝试在没有 ListView 的情况下使用此代码并且它有效但不幸的是它不适用于 ListView 。

最佳答案

不要使用异步任务来执行此操作。使用经过良好测试的图像加载器库。

我推荐https://github.com/koush/UrlImageViewHelper

我已经在三个备受瞩目的应用程序中使用了这个库,它为 50 万用户提供了出色的效果。

然后在您的 getView 方法中执行此操作

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

关于android - 在 ListView 中显示来自 URL 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14758072/

相关文章:

java - 我想在 Android 中的 SeekBar OnProgres 更改事件上设置图像的亮度?

java - Android - 图像缩放库

android - 替换 Android ViewPager 中的当前 Fragment

android - 如何使 Android 半透明主题更暗一点?

android - crashlytics 是否有公共(public) API?

android - 如果 USAGE_SHARED,Renderscript 在启用 GPU 的驱动程序上失败

Android如何在onitemclick覆盖函数中选择autocompletetextview的id

android - 有没有办法在多个嵌套的 RecyclerView 之间共享同一个 LayoutManager

android - 如何制作每行包含三个项目的 ListView ,这些项目是图像按钮,以及与该图像按钮对应的文本

java - fragment 中的 ImageView 未显示