我在那里有 ListView 的自定义适配器,我有下载位图的线程。它工作不好,因为当位图加载时它没有显示在列表视图中,如果我想看到它,我必须滚动列表。
所以问题是如何强制重新生成视图。在这种认识中,有很多困难。这有可能改变吗?
有我的代码:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class Adapter extends ArrayAdapter<Place> {
private Activity mActivity;
private List<Place> mList;
public Adapter(Activity context, List<Place> objects) {
super(context, R.layout.row, objects);
mActivity = context;
mList = objects;
}
static class ViewHolder {
private TextView mTextViewName;
private TextView mTextViewAddress;
private TextView mTextViewWeb;
private ImageView mImageButtonPhoto;
}
ViewHolder viewHolder;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflator = mActivity.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.mTextViewName = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.mTextViewAddress = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.mTextViewWeb = (TextView) convertView.findViewById(R.id.textView3);
viewHolder.mImageButtonPhoto = (ImageView) convertView.findViewById(R.id.imageButton1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mTextViewName.setText(mList.get(position).getName());
viewHolder.mTextViewAddress.setText(mList.get(position).getAddress());
viewHolder.mTextViewWeb.setText(mList.get(position).getUrl().toString());
viewHolder.mTextViewWeb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(mList.get(position).getUrl().toString()));
mActivity.startActivity(i);
}
});
URL url = mList.get(position).getPhoto_url();
final URL finalUrl = url;
new Thread(new Runnable() {
public void run() {
final Bitmap b;
try {
b = BitmapFactory.decodeStream(finalUrl.openConnection().getInputStream());
viewHolder.mImageButtonPhoto.post(new Runnable() {
public void run() {
viewHolder.mImageButtonPhoto.setImageBitmap(b);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
return convertView;
}
}