java 在带有文本和图像的 ListView 中使用 MatrixCursor 和 SimpleCursorAdapter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1882156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using MatrixCursor and SimpleCursorAdapter in a ListView with text and images
提问by Corey D
I'm having an issue using a MatrixCursorto populate my ListView:
我在使用 aMatrixCursor来填充我的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", ".99" });
SimpleCursorAdapter menuItems = new SimpleCursorAdapter(
this, R.layout.menu_row, menuCursor, menuCols, to);
setListAdapter(menuItems);
}
Constructing the SimpleCursorAdaptercauses a crash. Even when I tried removing the icon the app still crashed. Here is my menu_row.xml:
构建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>
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.access00(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.
我发现了问题,解决方案在下面的答案中。
采纳答案by Corey D
Let's chalk this one up to inexperience with debugging Java using Eclipse.
让我们将此归结为缺乏使用 Eclipse 调试 Java 的经验。
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.
在调试器中运行应用程序时,我因 RuntimeException 而崩溃。单击调用堆栈中的最顶部元素为我提供了变量列表,在该列表中我看到了异常 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.
具体错误是 InvalidArgument,因为我的 MatrixCursor 没有 _id 列。添加标记为 _id 的列解决了问题,现在一切正常。
Thanks for making me look at the debugger again! Be comfortable with and knowledgeable about your tools!
感谢您让我再次查看调试器!熟悉并熟悉您的工具!

