我正在尝试使用此示例为 ListView 创建一个 simpleAdapter。所以,我已经创建了所有内容,但是当我更改此行时
List<Movie> movies = getData2();
ListAdapter adapter = new MovieListAdapter(this, movies, android.R.layout.simple_list_item_2, new String[]{Movie.KEY_NAME, Movie.KEY_YEAR}, new int[]{android.R.id.text1, android.R.id.text2});
我收到了这个错误:
构造函数未定义
MovieListAdapter(ImdbApiActivity, List<Movie>, int, String[], int[])
这是相同的示例,我刚刚将 Cars 更改为 Movies。我知道这个例子是从 2009 年开始的,我的项目目标是 2.1。版本之间是否存在不兼容或有错误?
我的 simpleadapter 类如下所示:
public class MovieListAdapter extends SimpleAdapter {
private List < Movie > movies;
private int[] colors = new int[] {
0x30ffffff, 0x30808080
};
@SuppressWarnings("unchecked")
public MovieListAdapter(Context context, List <? extends Map < String, String >> movies,
int resource,
String[] from,
int[] to) {
super(context, movies, resource, from, to);
this.movies = (List < Movie > ) movies;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
我错过了一个错误吗?这是实现这一目标的“现代”方式?
编辑:更改上下文参数或列表均不适用于此示例。我的电影类扩展自 Hashmap。还有什么想法吗?谢谢!
import java.util.HashMap;
public class Movie extends HashMap {
public String year;
public String name;
public static String KEY_YEAR = "year";
public static String KEY_NAME = "name";
public Movie(String name, String year) {
this.year = year;
this.name = name;
}
@Override
public String get(Object k) {
String key = (String) k;
if (KEY_YEAR.equals(key))
return year;
else if (KEY_NAME.equals(key))
return name;
return null;
}
}