4

我想使用光标和列表视图显示我的表格。但我得到了错误。

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

但我没有在我的应用程序中声明 _id。有人可以帮助我吗?

这是我在 dbHelper 中的代码。

public Cursor DataPesanKeluar() {
    Cursor c = dba.rawQuery(
            " SELECT "
            + kel_id + ","
            + e_chiperteks + ","
            + k_nama + ","
            + kel_waktu +
            " FROM " + tbPesan + " INNER JOIN " + tbPesanKeluar +
            " ON "  + tbPesan + "." + p_idpesan + "=" + tbPesanKeluar + "." + kel_idpesan +
            " INNER JOIN " + tbEnkrip +
            " ON " + tbPesan + "." + p_idenkrip + "=" + tbEnkrip + "." + e_idenkrip +
            " INNER JOIN " + tbKontak + 
            " ON " + tbPesan + "." + p_idkontak + "=" + tbKontak + "." + k_id , null);
    return c;
}

这是我展示数据的课程。

listKeluar = (ListView)findViewById(R.id.listKeluar);

    String [] keluar = { data.k_nama, data.m_chiperteks, data.kel_waktu };
    int[] k = { R.id.tNama, R.id.tChiper, R.id.tWaktu };
    cursor = data.DataPesanKeluar();
    SimpleCursorAdapter keluarAdapter = new SimpleCursorAdapter( this, R.layout.baris_keluar, cursor, keluar, k ); //this is my error
    listKeluar.setAdapter(keluarAdapter);
    listKeluar.setOnItemClickListener( new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
            Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
            String idkeluar = listCursor.getString(listCursor.getColumnIndex(data.kel_id));
            String nama = listCursor.getString(listCursor.getColumnIndex(data.k_nama));
            String chiperteks = listCursor.getString(listCursor.getColumnIndex(data.m_chiperteks));
            String waktu = listCursor.getString(listCursor.getColumnIndex(data.kel_waktu));
4

4 回答 4

14

游标必须包含一个名为 _id 的列,否则此类将不起作用。

您也许可以尝试使用您现有的 ID 来伪造它:

Cursor c = dba.rawQuery(
        " SELECT "
        + kel_id + " AS _id,"
        + kel_id + ","
        + e_chiperteks + ","
        + k_nama + ","
        + kel_waktu +
        " FROM " + tbPesan + " INNER JOIN " + tbPesanKeluar +
        " ON "  + tbPesan + "." + p_idpesan + "=" + tbPesanKeluar + "." + kel_idpesan +
        " INNER JOIN " + tbEnkrip +
        " ON " + tbPesan + "." + p_idenkrip + "=" + tbEnkrip + "." + e_idenkrip +
        " INNER JOIN " + tbKontak + 
        " ON " + tbPesan + "." + p_idkontak + "=" + tbKontak + "." + k_id , null);
于 2013-05-05T07:47:28.147 回答
5

将 key_id 设为“_id”

public Cursor DataPesanKeluar() {
Cursor c = dba.rawQuery(
        " SELECT "
        + kel_id + " AS _id" + ","
        + e_chiperteks + ","
        + k_nama + ","
        + kel_waktu +
        " FROM " + tbPesan + " INNER JOIN " + tbPesanKeluar +
        " ON "  + tbPesan + "." + p_idpesan + "=" + tbPesanKeluar + "." + kel_idpesan +
        " INNER JOIN " + tbEnkrip +
        " ON " + tbPesan + "." + p_idenkrip + "=" + tbEnkrip + "." + e_idenkrip +
        " INNER JOIN " + tbKontak + 
        " ON " + tbPesan + "." + p_idkontak + "=" + tbKontak + "." + k_id , null);
return c;

}

于 2013-05-05T07:48:15.847 回答
0

只需从您的选择语句中删除 kel_id + ",",因为您尝试从中获取数据的表不包含“_id”列。

于 2013-05-05T07:45:11.050 回答
0

添加_id INTEGER PRIMARY KEY AUTOINCREMENT到您的创建表语句。Android 有时需要 _id 字段

于 2013-05-05T07:45:44.433 回答