3

我陷入了一个问题,我希望你能给我一些关于如何改进我的代码的建议。我正在编码关于将图像从 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;
    }
}

}

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

4

1 回答 1

14

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

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

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

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

UrlImageViewHelper.setUrlDrawable((ImageView) findViewById(R.id.ImageView), imgUrls[position]);
于 2013-02-07T18:18:30.523 回答