首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ListView的自定义适配器(Android )中从URL加载图像

在ListView的自定义适配器(Android )中从URL加载图像
EN

Stack Overflow用户
提问于 2015-04-29 21:22:14
回答 2查看 6.8K关注 0票数 4

虽然位图看起来是正确的,但是变量'userBitmap‘将保持为空。不过,在我的平板电脑上向上或向下滚动时,新的列表行将包含图片,但它们都是相同的,而且都是错误的。真的真的很困惑。我尝试过许多不同的方法从网络上获取图像。任何帮助都是非常感谢的。

我的自定义适配器:

代码语言:javascript
复制
public class MessagesArrayAdapter extends ArrayAdapter<ChatData>
{
    Bitmap userBitmap;

public MessageArrayAdapter(Context context, List<ChatData> objects)
{
    super(context, 0, objects);
}

public View getView(int position, View convertView, ViewGroup parent)
{
    ChatCell chatCell = new ChatCell();

    LayoutInflater inflater = LayoutInflater.from(getContext());
    convertView = inflater.inflate(R.layout.cell_chat, parent, false);

    chatCell.usernameTextView = (TextView) convertView.findViewById(R.id.usernameTextView);
    chatCell.messageTextView = (TextView) convertView.findViewById(R.id.messageTextView);
    chatCell.userImageView = (ImageView) convertView.findViewById(R.id.userImageView);

    ChatData chatData = getItem(position);

    // Get user image from web
    new loadImageAsync().execute(chatData.avatarURL);

    chatCell.userImageView.setImageBitmap(userBitmap);
    chatCell.usernameTextView.setText(chatData.username);
    chatCell.messageTextView.setText(chatData.message);

    return convertView;
}



private static class ChatCell
{
    TextView usernameTextView;
    TextView messageTextView;
    ImageView userImageView;
}

private class loadImageAsync extends AsyncTask<String, Void, Double>{
    @Override
    protected Double doInBackground(String... params) {

        userBitmap = loadImage(params[0]);


        return null;
    }
}


public Bitmap loadImage(String str) {

    InputStream instream = null;
    try {
        HttpGet httpRequest = new HttpGet(URI.create(str));
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
        HttpEntity entity = response.getEntity();
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
        instream = bufHttpEntity.getContent();
        Bitmap myBitmap = BitmapFactory.decodeStream(instream);

        return myBitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        //close input
        if (instream != null) {
            try {
                instream.close();
            } catch (IOException ioex) {
                // Handle error
            }
        }
    }

}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-29 21:40:55

解决问题的最简单方法是使用一些库,例如毕加索。它不仅更容易使用和使您的代码更易于阅读,而且还防止您在图像太大时出现OutOfMemory异常。然后,加载图像只需一行:

代码语言:javascript
复制
Picasso.with(context)
.load(urlOfYourImage)
.resize(50, 50) // here you resize your image to whatever width and height you like
.into(imageView)
票数 7
EN

Stack Overflow用户

发布于 2015-04-29 21:30:34

我认为你在这里遇到了两个问题:

1)视图回收--我自己也是Android的新手,这在使用listview时经常出现,当listview的部分移出屏幕时,它们在回滚时可能会出错。我个人有一个问题,按钮状态改变为错误的行时,滚动在一个。本教程更详细地介绍了什么是回收,并在为我工作的View私有类中提供了一个解决方案。我相信其他部分也会讨论这件事。

2) Image -这肯定是空的,还是没有出现在列表视图中?如果是前者,LoadImage()中的某些步骤一定不能正常工作。如果是后者,您可能需要在设置图像后尝试调用notifyDataSetChanged(),以告诉主线程图像已准备好绘制。如下所示(在AsyncTask中):

代码语言:javascript
复制
@Override
protected void onPostExecute() {
       notifyDataSetChanged();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29954830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档