New to android here, (and programming in general)
I’m following this example trying to create a lazy image loading adapter: http://thinkandroid.wordpress.com/2012/06/13/lazy-loading-images-from-urls-to-listviews/
I’m using “Album” instead of Student, but the rest is pretty much the same.
When I call the following from my adapter:
Album getItem(int position) {
return items.get(position);
}
I get “Type mismatch: cannot convert from Object to Album”. I tried copying the code from the example verbatim, and I got the same error..
Could somebody please explain what I am doing wrong, and how to fix it? Thank you.
Here is the code:
Album class:
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 Class:
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;
}
}
You have a generic
List, which will containObjectinstances. That means any instance of theObjectclass or a class that extendsObject(essentially all of them). When you attempt to get an item out of that list, the compiler knows that it’s anObject, but your method signature is saying you’re returning anAlbum; the compiler doesn’t know that the element you’re retrieving from the list definitely is anAlbum, so it can’t compile the code.You can solve this is one of two ways. You can declare your
Listto only contain instances of theAlbumclass:Or you can cast the object you’re retrieving from the list, to tell the compiler that you are definite that it’s an
Albumeven if it isn’t: