我正在嘗試這個example爲ListView創建一個simpleAdapter。所以,我得到了所有創建的,但是當我改變這一行Android定製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。版本之間是否存在不兼容或存在錯誤?
我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;
}
}
我想,List <?在您的自定義適配器構造函數參數中擴展Map>影片應更改爲列表影片。嘗試一下! –
Hiral
請檢查您是否通過正確的上下文。看起來你正在傳遞錯誤的上下文。 – VendettaDroid