gpt4 book ai didi

java - Android 中 SimpleAdapter 上的 ClassCastException

转载 作者:行者123 更新时间:2023-12-01 14:39:13 25 4
gpt4 key购买 nike

我试图在 Android 应用程序中显示警报列表,但遇到异常。我该如何解决这个问题?

这是我尝试初始化适配器的地方:

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

}

如果我不转换列表变量 (List<? extends Map<String, ?>>)我收到一个错误,迫使我转换它。我不太确定在这里做什么。

这是我的 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

最佳答案

这就是 SimpleAdapter在寻找。构造函数

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

其中数据是列表:

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".

您的解决方案,如果您想为 ListView 中的项目渲染对象,您可以使用其他适配器。或者,您可以将 Alarm 对象转换为 Map。

关于java - Android 中 SimpleAdapter 上的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16157853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com