我已经定义了列表视图,并与布局 xml 文件中的列表视图的 ID 正确链接。我仍然收到上述错误。
我研究了这里发布的类似问题,但无济于事。
请帮忙。
public class ImageList extends ListActivity {
final static String imageUrls[] = { //some urls
};
private List<Photo> model = new ArrayList<Photo>();
PhotoAdapter adapter = null;
ListView list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imagelist);
list = (ListView) findViewById(R.id.lvImageList);
// ArrayList<Bitmap> images = new ArrayList<Bitmap>();
adapter = new PhotoAdapter(this, android.R.layout.simple_list_item_1,
model);
list.setAdapter(adapter);
ProduceImageList(imageUrls);
}
public void ProduceImageList(String[] imageUrls) {
int size = imageUrls.length;
ImageView imagePlace = null;
for (int i = 0; i < 4; i++) {
imagePlace = (ImageView) getListView().getChildAt(i);
downloadImage task = new downloadImage(imagePlace);
task.execute(imageUrls[i]);
}
}
//asynchronous downloading of images
public class downloadImage extends AsyncTask<String, Void, Bitmap> {
// private String url;
Photo p = new Photo();
private final WeakReference<ImageView> imageViewReference;
public downloadImage(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
String localUrl = params[0];
//here a bitmap download method is used which works fine separately
return BitmapDownload.DownloadBitmap(localUrl);
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
if (isCancelled())
result = null;
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
p.setPhotoPic(result);
adapter.add(p);
}
}
super.onPostExecute(result);
}
}
//class defined with getter and setter method
public class Photo {
private Bitmap pic = null;
public Bitmap getPhotoPic() {
return pic;
}
public void setPhotoPic(Bitmap incomingPic) {
this.pic = incomingPic;
}
}
//custom ArrayAdapter to hold images
class PhotoAdapter extends ArrayAdapter<Photo> {
public PhotoAdapter(Context context, int textViewResourceId,
List<Photo> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
PhotoHolder holder = null;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.imagelistrow, parent,false);
holder = new PhotoHolder(row);
row.setTag(holder);
} else
holder = (PhotoHolder) row.getTag();
holder.populateFrom(model.get(position));
return (row);
}
}
//holder for the row view
static class PhotoHolder {
private ImageView imagePlace = null;
private View row = null;
PhotoHolder(View row) {
this.row = row;
imagePlace = (ImageView) row.findViewById(R.id.ivImageList);
}
void populateFrom(Photo p) {
imagePlace.setImageBitmap(p.getPhotoPic());
}
}
}
我的看法是...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<ListView android:id="@+id/lvImageList" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>