在带有文本和图像的 ListView 中使用 MatrixCursor 和 SimpleCursorAdapter

发布于 2024-08-13 18:07:11 字数 2472 浏览 3 评论 0原文

我在使用 MatrixCursor 填充 ListView 时遇到问题:

private void fillData() {
    String[] menuCols = new String[] { "icon", "item", "price" };
    int[] to = new int[] { R.id.icon, R.id.item, R.id.price };

    MatrixCursor menuCursor = new MatrixCursor(menuCols);
    startManagingCursor(menuCursor);

    menuCursor.addRow(new Object[] { R.drawable.chicken_sandwich, "Chicken Sandwich", "$3.99" });

    SimpleCursorAdapter menuItems = new SimpleCursorAdapter(
            this, R.layout.menu_row, menuCursor, menuCols, to);

    setListAdapter(menuItems);
}

构造 SimpleCursorAdapter 会导致崩溃。即使我尝试删除图标,应用程序仍然崩溃。这是我的 menu_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView 
        android:id="@+id/icon"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </ImageView>
    <TextView 
        android:id="@+id/item" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
    <TextView 
        android:id="@+id/price" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

编辑:这是崩溃时的调用堆栈:

Thread [<3> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2481  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2497   
    ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
    ActivityThread$H.handleMessage(Message) line: 1848  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4338    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 860  
    ZygoteInit.main(String[]) line: 618 
    NativeStart.main(String[]) line: not available [native method]  

解决方案:

我发现了问题和解决方案在我下面的回答中。

I'm having an issue using a MatrixCursor to populate my ListView:

private void fillData() {
    String[] menuCols = new String[] { "icon", "item", "price" };
    int[] to = new int[] { R.id.icon, R.id.item, R.id.price };

    MatrixCursor menuCursor = new MatrixCursor(menuCols);
    startManagingCursor(menuCursor);

    menuCursor.addRow(new Object[] { R.drawable.chicken_sandwich, "Chicken Sandwich", "$3.99" });

    SimpleCursorAdapter menuItems = new SimpleCursorAdapter(
            this, R.layout.menu_row, menuCursor, menuCols, to);

    setListAdapter(menuItems);
}

Constructing the SimpleCursorAdapter causes a crash. Even when I tried removing the icon the app still crashed. Here is my menu_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView 
        android:id="@+id/icon"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </ImageView>
    <TextView 
        android:id="@+id/item" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
    <TextView 
        android:id="@+id/price" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

Edit: Here is the call stack at the time of the crash:

Thread [<3> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2481  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2497   
    ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
    ActivityThread$H.handleMessage(Message) line: 1848  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4338    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 860  
    ZygoteInit.main(String[]) line: 618 
    NativeStart.main(String[]) line: not available [native method]  

SOLUTION:

I found the problem and the solution is in my answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-08-20 18:07:11

我们将这一问题归因于缺乏使用 Eclipse 调试 Java 的经验。

在调试器中运行应用程序时,我因运行时异常而崩溃。单击调用堆栈中最顶部的元素会显示变量列表,在其中我看到了异常 e。

具体错误是 InvalidArgument,因为我的 MatrixCursor 没有 _id 列。添加标记为 _id 的列解决了问题,现在一切正常。

感谢您让我再次查看调试器!熟悉并熟悉您的工具!

Let's chalk this one up to inexperience with debugging Java using Eclipse.

Running the application in the debugger, I crashed with a RuntimeException. Clicking the very top element in the call stack gave me the list of Variables, at which I saw my Exception e.

The specific error was an InvalidArgument because my MatrixCursor did not have an _id column. Adding a column labeled _id fixed the problem and now everything works.

Thanks for making me look at the debugger again! Be comfortable with and knowledgeable about your tools!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文