Question

I am trying to display a list of alarms in my Android application but I am getting an Exception. How can I resolve this?

This is where I am trying I initialise the adapter:

    List<Alarm> list;
    Database db = new Database(AlarmListActivity.this);
    list = db.getAllAlarms();

    if(!list.isEmpty() && list != null)
    {
        System.out.println("inside list");
        System.out.println(list);

        ListAdapter adapter = new SimpleAdapter(
                AlarmListActivity.this,
                (List<? extends Map<String, ?>>) list,
                R.layout.list_item, 
                new String[] { "id", "time"},
                new int[] { R.id.id, R.id.time});
        setListAdapter(adapter);

    }

If I do not cast the list variable (List<? extends Map<String, ?>>) I get an error which forces me to cast it. I'm not quite sure what to do here.

This is my LogCat:

04-22 22:26:20.192: E/AndroidRuntime(1697): FATAL EXCEPTION: main
04-22 22:26:20.192: E/AndroidRuntime(1697): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wakeup/com.example.wakeup.AlarmListActivity}: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.List
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.os.Looper.loop(Looper.java:137)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at java.lang.reflect.Method.invoke(Method.java:511)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at dalvik.system.NativeStart.main(Native Method)
04-22 22:26:20.192: E/AndroidRuntime(1697): Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.List
04-22 22:26:20.192: E/AndroidRuntime(1697):     at com.example.wakeup.AlarmListActivity.onCreate(AlarmListActivity.java:53)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.app.Activity.performCreate(Activity.java:5104)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-22 22:26:20.192: E/AndroidRuntime(1697):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-22 22:26:20.192: E/AndroidRuntime(1697):     ... 11 more
04-22 22:26:26.472: I/Process(1697): Sending signal. PID: 1697 SIG: 9
Was it helpful?

Solution

That is what SimpleAdapter is looking for. constructor

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

where data is the List of :

A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from".

Your Solution, If you want to render Objects for items in your ListView, you may what to use other Adapters. Or, you can convert your Alarm object in to Map.

OTHER TIPS

Change your List to ArrayList. As the message is telling you, you can't cast to a Map from a List here

(List<? extends Map<String, ?>>) list,

have a look at this SO answer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top