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

从ListView自定义适配器中的URL加载图像(Android Studio),android,image,url,android-listview,android-adapter,Android,Image,Url,Android Listview,Android Adapter,虽然位图似乎提取正确,但变量“userBitmap”将保持空。当在我的平板电脑上上上下滚动时,新的列表行将包含图片,但它们都是相同和错误的。真的,真的很困惑。我尝试了许多不同的方法从网络上获取图像。非常感谢您的帮助 我的自定义适配器: public class MessagesArrayAdapter extends ArrayAdapter<ChatData> { Bitmap userBitmap; public MessageArrayAdapter(Context

虽然位图似乎提取正确,但变量“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
            }
        }
    }

}
public class MessagesArrayAdapter扩展了ArrayAdapter
{
位图用户位图;
public MessageArrayAdapter(上下文、列表对象)
{
超级(上下文,0,对象);
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
ChatCell ChatCell=新ChatCell();
LayoutInflater充气器=LayoutInflater.from(getContext());
convertView=充气机。充气(R.layout.cell\u chat,父项,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(位置);
//从web获取用户图像
新建loadImageAsync().execute(chatData.avatarURL);
chatCell.userImageView.setImageBitmap(userBitmap);
chatCell.usernameTextView.setText(chatData.username);
chatCell.messageTextView.setText(chatData.message);
返回视图;
}
私有静态类聊天室
{
TextView用户名TextView;
文本视图消息文本视图;
ImageView用户ImageView;
}
私有类loadImageAsync扩展了AsyncTask{
@凌驾
受保护的双doInBackground(字符串…参数){
userBitmap=loadImage(参数[0]);
返回null;
}
}
公共位图加载图像(字符串str){
InputStream instream=null;
试一试{
HttpGet-httpRequest=newhttpget(URI.create(str));
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse response=(HttpResponse)httpclient.execute(httpRequest);
HttpEntity=response.getEntity();
BufferedHttpEntity bufHttpEntity=新的BufferedHttpEntity(实体);
instream=bufhttpenty.getContent();
位图myBitmap=BitmapFactory.decodeStream(流内);
返回我的位图;
}捕获(例外e){
e、 printStackTrace();
返回null;
}最后{
//闭合输入
如果(流内!=null){
试一试{
流内关闭();
}捕获(IOException ioex){
//处理错误
}
}
}
}

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

1) 视图回收-我本人对Android是新手,在使用listview时,这一点经常出现,当listview的某些部分移出屏幕时,它们在向后滚动时可能是错误的。我个人有一个问题,当在一行中上下滚动时,按钮状态改变为错误的行。更详细地讨论了什么是回收,并在一个视图持有者私有类中提供了一个解决方案,该类为我工作。我相信我们的其他部分也会谈论这个

2) 图像为空-这是否一定是空的,或者只是没有出现在listview中?如果是前者,则LoadImage()中的某些步骤一定无法正常工作。如果是后者,您可能希望在设置图像后尝试调用notifyDataSetChanged(),告诉主线程图像已准备好绘制。类似以下内容(在异步任务中):


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

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

我会使用并过上幸福的生活。谢谢你的评论。notifyDataSetChange();帮助。现在,所有视图都有图片。但是,它们都是一样的,每秒钟都在变化(而文本视图保持不变/正确)。我认为这与在getView()之外定义的“userBitmap”有关。有更好的方法吗?天哪。这是为我准备的,希望能有所帮助。简化一下一切都在一行代码中工作。真不敢相信。你认为我应该继续使用异步吗?在build.gradle:compile'com.squareup.picasso:picasso:2.5.2'中将这行代码添加到依赖项中
Picasso.with(context)
.load(urlOfYourImage)
.resize(50, 50) // here you resize your image to whatever width and height you like
.into(imageView)