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 !?