这里是 android 新手,(和一般编程)
我正在按照这个示例尝试创建一个惰性图像加载适配器:http ://thinkandroid.wordpress.com/2012/06/13/lazy-loading-images-from-urls-to-listviews/ 我正在使用“专辑”而不是学生,但其余的几乎相同。
当我从我的适配器调用以下内容时:
Album getItem(int position) {
return items.get(position);
}
我得到“类型不匹配:无法从对象转换为专辑”。我尝试从示例中逐字复制代码,但我得到了同样的错误..
有人可以解释我做错了什么,以及如何解决吗?谢谢你。
这是代码:
专辑类:
package another.music.player;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
public class Album {
private String album;
private String imageUri;
private Bitmap image;
private AlbumAdapter albumAdapter;
public Album(String album, String imageUri) {
this.album = album;
this.imageUri = imageUri;
// Default image
this.album = null;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getImageUri() {
return imageUri;
}
public void setImageUri(String imageUri) {
this.imageUri = imageUri;
}
public Bitmap getImage() {
return image;
}
public AlbumAdapter getAdapter() {
return albumAdapter;
}
public void setAdapter(AlbumAdapter albumAdapter) {
this.albumAdapter = albumAdapter;
}
public void loadImage(AlbumAdapter albumAdapter) {
// Hold a reference to the adapter
this.albumAdapter = albumAdapter;
if (imageUri != null && !imageUri.equals("")) {
new ImageLoadTask().execute(imageUri);
}
}
// Async task
private class ImageLoadTask extends AsyncTask<String, String, Bitmap> {
@Override
protected void onPreExecute() {
Log.i("ImageLoadTask", "Loading Image..");
}
// Param[0] is Image URI
protected Bitmap doInBackground(String... param) {
Log.i("ImageLoadTask", "Attempting to load image Uri: " + param[0]);
try {
Bitmap bitmap = ImageService.getBitmapFromURI(param[0]);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
protected void onProgressUpdate(String... progress) {
// No Op
}
protected void onPostExecute(Bitmap ret) {
if (ret != null) {
Log.i("ImageLoadTask", "Successfully loaded" + album + " image");
image = ret;
if (albumAdapter != null) {
// When image is loaded, notify the adapter
albumAdapter.notifyDataSetChanged();
}
} else {
Log.e("ImageLoadTask", "Failed to load " + album + " image");
}
}
}
}
专辑适配器类:
package another.music.player;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AlbumAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List items = new ArrayList();
public AlbumAdapter(Context context, List items) {
mInflater = LayoutInflater.from(context);
this.items = items;
}
public int getCount() {
return items.size();
}
public Album getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Album album = items.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(album.getName());
if (s.getImage() != null) {
holder.pic.setImageBitmap(s.getImage());
} else {
// MY DEFAULT IMAGE
holder.pic.setImageResource(R.drawable.generic_profile_man);
}
return convertView;
}
static class ViewHolder {
TextView name;
ImageView pic;
}
}