【问题标题】:Displaying images from URL in Listview在 Listview 中显示来自 URL 的图像
【发布时间】:2013-02-07 18:12:26
【问题描述】:

我遇到了一个问题,希望您能就如何改进我的代码给我一些建议。我正在编码关于将图像从 URL 显示到列表视图中,但它没有出现。这是我的代码:

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

}

您对如何改进我的代码有什么建议吗?我尝试在没有列表视图的情况下使用此代码,它可以工作,但不幸的是它不适用于列表视图。

【问题讨论】:

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


【解决方案1】:

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

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

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

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

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

【讨论】:

  • UrlImageViewHelper 为我节省了大量时间,感谢您的提示
  • 不客气,但这些天,我推荐的是 Square 的 Picasso。
  • 我现在来看看。你能快速告诉我你决定改用毕加索的理由吗?
  • Picasso 由 Square 管理,而 UrlImageViewHelper 由 Koushik Doutta 管理。一个团队对一个人。此外,我深入研究了两者的代码,发现 Picasso 更加健壮。
猜你喜欢
  • 2011-09-12
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 2017-10-04
  • 2014-06-04
  • 2017-04-20
  • 2018-12-13
相关资源
最近更新 更多