首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓定制SimpleAdapter

安卓定制SimpleAdapter
EN

Stack Overflow用户
提问于 2012-08-29 03:53:29
回答 2查看 7.3K关注 0票数 0

我正在尝试这个示例,用于为ListView创建一个simpleAdapter。所以,我被创造了,但是当我改变了这一行

代码语言:javascript
复制
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我的项目。版本之间是不兼容的,还是有错误?

我的简单适配器类如下所示:

代码语言:javascript
复制
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扩展而来。还有别的主意吗?谢谢!

代码语言:javascript
复制
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;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2012-08-29 04:28:42

这似乎是自定义适配器类构造函数参数中的问题。

您已经将它定义为List<? extends Map<String, String>>电影,其中您要从List<Movies>对象中传递activity.That对象,这就是为什么它告诉您,没有定义这样的构造函数。

尝试将建筑参数改为List<Movies> movies,这样可以解决你的问题,我想!

票数 2
EN

Stack Overflow用户

发布于 2012-08-29 04:06:42

试着改变

代码语言:javascript
复制
`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});`

代码语言:javascript
复制
`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()返回的上下文。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12170632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档