【问题标题】:Android widget : how to load image from web in a listview?Android 小部件:如何在列表视图中从网络加载图像?
【发布时间】:2013-02-27 16:06:53
【问题描述】:

小部件说明:

小部件由一个列表视图组成。此列表视图的每个项目都有不同的属性:

  • 标题
  • 图片网址
  • 价格
  • ...

问题:

使用每个项目的 url,我编写了一些代码来从网络加载图像,图像很好地显示在我的列表视图中,但是当我想滚动我的列表视图时,所有项目都消失了,系统尝试加载其他相应的图像显示的项目。

代码示例:

这是我执行这些操作的 getViewAt() 方法的代码:

public RemoteViews getViewAt(int position) {

    WidgetItem item = mWidgetItems.get(position);
    // We construct a remote views item based on our widget item xml file, and set the
    // text based on the position.

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
    String theme = settings.getString("choixtheme", "default");

    if (theme.equals("default")) {
        rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item_default);
    }

    if (item.urlImage != "") {
            URL url = new URL(urlImage);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);   
    }       

    // Return the remote views object.
    return rv;
}

【问题讨论】:

    标签: listview android-widget uiimageview httpurlconnection


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多