【问题标题】:Retrieving data from SQLite and display it into a ListView从 SQLite 检索数据并将其显示到 ListView
【发布时间】:2015-12-13 18:21:05
【问题描述】:

我对这段代码有疑问:

SimpleCursorAdapter adapter;
       adapter = new SimpleCursorAdapter(context, R.layout.activity_view_medication, cursor, from, toViewMed)

当我运行我的项目时,logcat 会显示:

java.lang.IllegalArgumentException: column '_id' does not exist

代码:

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context=this;

    // Get ListView object from xml
    lv = (ListView)findViewById(R.id.list);

    myDb = new DatabaseHelper(this);

    Create_Alarm();
    populateListView();
}

public void populateListView(){

    myDb.getAllData();
    Cursor cursor = myDb.getAllData();

    String[] from = new String[] {myDb.COL_2, myDb.COL_3 };
    int[] toViewMed = new int[] {R.id.txtName, R.id.txtQuantity};

    SimpleCursorAdapter adapter;
    adapter = new SimpleCursorAdapter(context, R.layout.activity_view_medication, cursor, from, toViewMed) {

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            //return null;
            return LayoutInflater.from(context).inflate(R.layout.activity_view_medication, parent, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            // Find fields to populate in inflated template
            TextView medicine = (TextView) view.findViewById(R.id.txtName);
            TextView quantity= (TextView) view.findViewById(R.id.txtQuantity);

            // Extract properties from cursor
            String  from = cursor.getString(cursor.getColumnIndexOrThrow("from"));
            int toViewMed = cursor.getInt(cursor.getColumnIndexOrThrow("toViewMed"));

            // Populate fields with extracted properties
            medicine.setText(from);
            quantity.setText(String.valueOf(toViewMed));

        }
    };

    // Assign adapter to ListView
    lv.setAdapter(adapter);
}

【问题讨论】:

  • 错误提示您的表没有名为“_id”的列

标签: java android sqlite android-listview simplecursoradapter


【解决方案1】:

CursorAdapters 需要 _id 列,因为它们依赖它。
快速修复:添加到您的查询 ... , rowID AS _id, ...(显然不是点)

rowID 是一个特殊的“自动”字段,它确实可以替代缺少的 _id 字段。

根据official docs

在 SQLite 中,表行通常有一个 64 位有符号整数 ROWID,它在同一个表的所有行中是唯一的。 (没有 ROWID 表是例外。)

您可以使用特殊列名 ROWID、ROWID 或 OID 之一访问 SQLite 表的 ROWID。除非您声明一个普通表列使用这些特殊名称之一,否则该名称的使用将引用声明的列而不是内部 ROWID。

如果表包含 INTEGER PRIMARY KEY 类型的列,则该列将成为 ROWID 的别名。然后,您可以使用四个不同名称中的任何一个来访问 ROWID,即上面描述的原始三个名称或赋予 INTEGER PRIMARY KEY 列的名称。所有这些名称都是彼此的别名,并且在任何情况下都同样适用。

【讨论】:

  • 我已经改变了这样的代码: *public Cursor getAllData(){ SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor= db.rawQuery("select rowid _id, from TABLE_NAME", null); ** 返回光标;}
  • 没有。应该是:... db.rawQuery("select rowid _id, from " + TABLE_NAME, null);
【解决方案2】:

你也需要检查你的数据库。

试试这个

例子:

MyDatabase.java

public class MyDatabaseHelper extends SQLiteOpenHelper {

    public static final rowid_id="_id";
       ......

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " ( " + rowid_id + " INTEGER PRIMARY KEY ,Name TEXT,Weather TEXT, Date DATETIME, Status Text, TimeIn_Info DATE TIME, TimeOut_Info DATETIME)");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多