简体   繁体   中英

Android custom SimpleAdapter

I'm trying this example for creating a simpleAdapter for a ListView. 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. Is there a incompatibility between versions or is there an error?

My simpleadapter class looks like this:

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. 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>> movies where you are passing List<Movies> object from the activity.That is why it is telling you,such constructor is not defined.

Try changing parameter of constuctor to List<Movies> movies ,that would resolve your problem,i think!

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.

YourClass.this is a reference to an instance of the YourClass.class . You can also send context returned by getApplicationContext()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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