android studio常用适配器,从ListView(Android Studio)的自定义适配器中的U...

虽然位图似乎是正确的,但变量’userBitmap’将保持为null.但是,当我在平板电脑上向上或向下滚动时,新的列表行将包含图片,但它们都是相同和错误的.真的,真的很困惑.我尝试了许多不同的方法从网上获取图像.任何帮助是极大的赞赏.

我的定制适配器:

public class MessagesArrayAdapter extends ArrayAdapter

{

Bitmap userBitmap;

public MessageArrayAdapter(Context context, List 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{

@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

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值