我正在尝试这个示例,用于为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[])
同样的例子,我刚换了车看电影。我知道这个例子来自2009年,我的目标是2.1我的项目。版本之间是不兼容的,还是有错误?
我的简单适配器类如下所示:
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;
}
}发布于 2012-08-29 04:28:42
这似乎是自定义适配器类构造函数参数中的问题。
您已经将它定义为List<? extends Map<String, String>>电影,其中您要从List<Movies>对象中传递activity.That对象,这就是为什么它告诉您,没有定义这样的构造函数。
尝试将建筑参数改为List<Movies> movies,这样可以解决你的问题,我想!
发布于 2012-08-29 04:06:42
试着改变
`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});`至
`ListAdapter adapter = new MovieListAdapter(YourClass.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});`因为this可能引用不同的实例。
YourClass.this是对YourClass.class实例的引用。还可以发送getApplicationContext()返回的上下文。
https://stackoverflow.com/questions/12170632
复制相似问题