Error to populate listview with cursor and sqlite

1

I want to populate a listview with information from a sqllite table, but it gives me an error.

This is the structure of the project

In fragment clients I have the call to the cursor in the following way

 private class ClienteLoadTask extends AsyncTask<Void, Void, Cursor> {

    @Override
    protected Cursor doInBackground(Void... voids) {
       // showMessage("Leyendo Clientes");
        return mClienteDbHelper.getAllClientes();
    }

    @Override
    protected void onPostExecute(Cursor cursor) {
        if (cursor != null && cursor.getCount() > 0) {
           showMessage("Hay datos en la base :  "+ cursor.getCount());
           // mClienteAdapter.swapCursor(cursor);
        } else {
            // Mostrar empty state
            showMessage("No hoy datos en la base "+ cursor.getCount());
        }
    }
}

The ClientCursorAdapter only has to fill each item in the list

In the client class, declare a constructor with the cursor in the following way

public cliente(Cursor cursor){
    cMAId=cursor.getString(cursor.getColumnIndex(clienterEntry.C_id));
    cMACODIGO = cursor.getString(cursor.getColumnIndex(clienterEntry.C_codigo));
    cMANOMBRE = cursor.getString(cursor.getColumnIndex(clienterEntry.C_nombre));
    cMADIRECCION= cursor.getString(cursor.getColumnIndex(clienterEntry.C_direccion));
    cMACIUDAD= cursor.getString(cursor.getColumnIndex(clienterEntry.C_ciudad));
    cMACANAL= cursor.getString(cursor.getColumnIndex(clienterEntry.C_canal));
    cMALONGITUD=cursor.getString(cursor.getColumnIndex(clienterEntry.C_longitud));
    cMALATITUD = cursor.getString(cursor.getColumnIndex(clienterEntry.C_latitud));
}

In the dbHelper I read the table as follows

 public Cursor getAllClientes() {
    return getReadableDatabase()
            .query(
                    clienterEntry.TABLE_CLIENTE,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null);
}

When I run the app it closes and the logcat comes out

04-21 12:28:48.722 18484-18484/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.cspm.ventas4.cspm, PID: 18484
                                               java.lang.IllegalArgumentException: column '_id' does not exist
                                                   at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
                                                   at android.widget.CursorAdapter.swapCursor(CursorAdapter.java:378)
                                                   at com.cspm.ventas4.cspm.ClientesFragmet$ClienteLoadTask.onPostExecute(ClientesFragmet.java:106)
                                                   at com.cspm.ventas4.cspm.ClientesFragmet$ClienteLoadTask.onPostExecute(ClientesFragmet.java:94)
                                                   at android.os.AsyncTask.finish(AsyncTask.java:688)
                                                   at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:705)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:156)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6524)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

Where am I doing wrong !?

    
asked by Alldesign Web 21.04.2017 в 19:29
source

2 answers

0

If someone serves you, the solution was that creating the table the id should be _id so that the listview with the adapter can read the data.

public static final String TABLE_CLIENTE = "clientes";
    public static final String Cid = "_id";
    public static final String Ccodigo = "codigo";
    public static final String Cnombre = "nombre";
    public static final String Cdireccion = "direccion";
    public static final String Cciudad = "ciudad";
    public static final String Ctipo= "tipo";
    public static final String Ccanal= "canal";
    public static final String Clongitud= "longitud";
    public static final String Clatitud= "latitud";

That was it.

    
answered by 26.04.2017 / 18:23
source
0

The column _id does not exist, check the query with which you created your table, if you performed a test you may have to delete the application and upload it again to create the correct structure.

  

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

    
answered by 21.04.2017 в 20:20