Android的ListView的数据库异常 [英] Android ListView Database Exception
问题描述
我一直是个顽皮的男孩,我已经复制从Android开发者官方网站的记事本应用程序的方法,这是我的类:
I have been a naughty boy and I've copied a method from official Notepad application from android developer site, this is my class :
package com.example.prva;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
public class ListView extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
fillData();
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = DatabaseManager.getAllData();
startManagingCursor(c);
String[] from = new String[] { DatabaseManager.TABLE_COLUMN_ONE };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
}
当我尝试运行这个ListActivity我得到这个错误:
When I try to run this ListActivity I get this error :
01-31 02:39:14.259: E/AndroidRuntime(1845): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prva/com.example.prva.ListView}: java.lang.IllegalArgumentException: column '_id' does not exist
现在我明白了这一点,因为它的真实,我没有_id列在我的数据库(记事本应用程序数据库中有它,使用它,但我有我自己的数据库),我只是不明白的地方是提到,色谱柱在我的ListActivity类?它在哪里被从那么它给人的错误叫什么名字?
Now I understand this because its true, I do not have the _id column in my database (the notepad application database has it and used it but I have my own database), I just don't understand where is that column mentioned in my ListActivity class? Where is it being called from so it gives the error?
推荐答案
查看文档的的CursorAdapter :
光标必须包含一个指定的列_id或本级将无法正常工作。
The Cursor must include a column named _id or this class will not work.
在你的code,使用SimpleCursorAdapter,这是一个派生类,所以会出现这种说法适用。
In your code, you use the SimpleCursorAdapter, which is a derived class, so it appears this statement applies.
光标是像迭代器和指针,它们仅仅包含了一种机制,横切数据,它们不包含列本身。
Cursors are like iterators or pointers, they contain nothing but a mechanism for transversing the data, they contain no columns themselves.
从另一个文档,你可以了解上述更好的语句:
From another documentation, you can understand the statement above better:
内容处理URI标识
按照惯例,供应商提供访问单个行表中的由 接受内容的URI与该行的ID值时的结束 URI。同样按照约定,供应商ID值表的匹配 _ID列,并执行对符合该行的请求的访问。
By convention, providers offer access to a single row in a table by accepting a content URI with an ID value for the row at the end of the URI. Also by convention, providers match the ID value to the table's _ID column, and perform the requested access against the row that matches.
该公约有利于一个常见的设计模式的应用程序访问 的提供者。该应用程序做了查询,对供应商并显示 导致光标在使用CursorAdapter的一个ListView。定义 的CursorAdapter的要求在光标的一列是_ID。
This convention facilitates a common design pattern for apps accessing a provider. The app does a query against the provider and displays the resulting Cursor in a ListView using a CursorAdapter. The definition of CursorAdapter requires one of the columns in the Cursor to be _ID.
多种方式可以解决这个问题:
Several ways for you to fix the problem:
-
添加列_id在表中。
Add a column "_id" in your table.
使用别名来获得curosr。例如:
Using alias to get the curosr. For example:
选择someid为_id,姓名,FROM表1号;
SELECT someid as _id, name, number FROM TABLE1;
这篇关于Android的ListView的数据库异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!