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

标签 android image url android-listview android-adapter

虽然似乎正确获取了位图,但变量“userBitmap”将保持为空。但是,当在我的平板电脑上向上或向下滚动时,新的列表行将包含图片,但它们都是一样的而且是错误的。真的,真的很迷茫。我尝试了多种从网络获取图像的不同方法。非常感谢任何帮助。

我的自定义适配器:

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

}

最佳答案

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

Picasso.with(context)
.load(urlOfYourImage)
.resize(50, 50) // here you resize your image to whatever width and height you like
.into(imageView)

关于android - 在 ListView (Android Studio) 的自定义适配器中从 URL 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29954830/

相关文章:

android - SpotsDialog 在 'dmax.dialog.SpotsDialog 中具有私有(private)访问权限

java - 为 Google Play 以外的应用显示 Admob 广告

html - 贴在圆 Angular 上的 Chrome 悬停效果

javascript - 如何使用 jQuery 和复选框添加 url 参数末尾?

JQuery:如果我从 http://更改为 https://,如何更改 iframe 的 url

android - Android string.xml 中的类转换异常

android - 在 android sql-lite 中查询以查找在保存为年、月和日的两个日期之间进行的条目

css - 如何在使用 onClick 动态更改图像时将图像缩放到现有 div 的大小?

ios - 是否可以在不创建自定义单元格类的情况下更改单元格图像?

java - 单击 jtable 中的超链接?