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

我只是一个虾纸丫 提交于 2019-11-28 04:55:29

问题


i want to show my table using cursor and list view. but i got error.

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

but i didn't declare _id in my application. can somebody help me?

this is my code in 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;
}

and this is my class to display data.

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));

回答1:


The Cursor must include a column named _id or this class will not work.

You can maybe try to fake it using your existing 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);



回答2:


Make key_id as "_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;

}




回答3:


Just remove kel_id + "," from your select statement because the table from which your are trying to get the data does'nt contain '_id' column.




回答4:


Add _id INTEGER PRIMARY KEY AUTOINCREMENT to your create table statement. Android sometimes requires the _id field



来源:https://stackoverflow.com/questions/16382416/caused-by-java-lang-illegalargumentexception-column-id-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!