简体   繁体   English

Android自定义SimpleAdapter

[英]Android custom SimpleAdapter

I'm trying this example for creating a simpleAdapter for a ListView. 我正在尝试这个例子为ListView创建一个simpleAdapter。 So, I got all created but when I change this line 所以,我创造了所有,但当我改变这一行

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});

I got this error: 我收到了这个错误:

The constructor is undefined 构造函数未定义

MovieListAdapter(ImdbApiActivity, List<Movie>, int, String[], int[])

It's the same example, I've just changed Cars for Movies. 这是同样的例子,我刚刚改变了汽车电影。 I know that this example is from 2009, my target is 2.1 on my project. 我知道这个例子是从2009年开始的,我的目标是2.1。 Is there a incompatibility between versions or is there an error? 版本之间是否存在不兼容性或是否存在错误?

My simpleadapter class looks like this: 我的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;
    }
}

Is there an error i'm missing? 我错过了一个错误吗? which is the "modern" way to achieve this? 这是实现这一目标的“现代”方式吗?

Edit: Neither changing the context parameter or the List worked for this example. 编辑:更改上下文参数或列表都不适用于此示例。 My Movie class extends from Hashmap. 我的Movie类从Hashmap扩展而来。 Any other idea? 还有其他想法吗? thanks! 谢谢!

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;
    }
}

It seems to be problem in the parameter of your custom adapter class constructor. 它似乎是自定义适配器类构造函数的参数中的问题。

You have defined it as List<? extends Map<String, String>> 您已将其定义为List<? extends Map<String, String>> List<? extends Map<String, String>> movies where you are passing List<Movies> object from the activity.That is why it is telling you,such constructor is not defined. List<? extends Map<String, String>>电影,你从活动中传递List<Movies>对象。这就是它告诉你的原因,没有定义这样的构造函数。

Try changing parameter of constuctor to List<Movies> movies ,that would resolve your problem,i think! 尝试将constuctor的参数更改为List<Movies> movies ,这可以解决您的问题,我想!

Try changing 尝试改变

`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});`

to

`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});`

because this may be referencing to the different instance. 因为this可能是引用不同的实例。

YourClass.this is a reference to an instance of the YourClass.class . YourClass.this是对YourClass.class实例的引用。 You can also send context returned by getApplicationContext() 您还可以发送getApplicationContext()返回的上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM