这里的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");
}
}
}
}
AlbumAdapter类:
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;
}
}
答案 0 :(得分:2)
您有一个通用List
,其中包含Object
个实例。这意味着Object
类的任何实例或扩展Object
的类(基本上都是它们)。当您尝试从该列表中获取项目时,编译器知道它是Object
,但您的方法签名表示您正在返回Album
;编译器不知道您从列表中检索的元素肯定是是Album
,因此它无法编译代码。
你可以解决这两种方式之一。您可以声明List
仅包含Album
类的实例:
List<Album> items = new ArrayList<Album>();
或者你可以从列表中转换你正在检索的对象,告诉编译器你确定它是Album
,即使它不是:
Album getItem(int position) {
return (Album)items.get(position);
}
答案 1 :(得分:1)
尝试以这种方式列出您的列表:
List<Album> items = new ArrayList<Album>();
答案 2 :(得分:1)
您应该使用List / ArrayList的专用形式,或者自己投射结果。
Album getItem(int position) {
return (Album)items.get(position);
}
或更好:
private List<Album> items = new ArrayList<Album>();