I have a detail that with my app that I have in android studio.
The detail is that I get some data through webservices and store them in a local bd sqlite and I want to show them in a listview with a custom adapter, the problem is when the query is made to get the data later to insert them , when I'm making the query the app thunders and sends me the following error:
Caused by: java.lang.IllegalStateException: Could not read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetString (Native Method) at android.database.CursorWindow.getString (CursorWindow.java:434) at android.database.AbstractWindowedCursor.getString (AbstractWindowedCursor.java:51) at mx.com.oncontrol.oncontrol.SqliteDB.ObtainClients (SqliteDB.java:95) at mx.com.oncontrol.oncontrol.Clientes.UpdateList (Clientes.java:293) at mx.com.oncontrol.oncontrol.Clientes.onCreate (Clientes.java:61) at android.app.Activity.performCreate (Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2148)
I leave my database code
public class SqliteDB {
private DBhelper oBD;
private SQLiteDatabase db;
public SqliteDB(Context context)
{
oBD = new DBhelper(context);
}
public void CloseDB()
{
if (db.isOpen())
{
db.close();
}
}
public boolean isOpenDB()
{
return(db.isOpen());
}
public long executeSQL(String sql, Object[] bindArgs)
{
long iRet = 0;
db = oBD.getWritableDatabase();
db.execSQL(sql, bindArgs);
CloseDB();
return(iRet);
}
public Cursor querySql(String sql, String[] selectionArgs)
{
Cursor oRet = null;
db = oBD.getReadableDatabase();
oRet = db.rawQuery(sql, selectionArgs);
return (oRet);
}
private int Empresa;
private int Cliente;
private String ClienteClave;
private String ClienteDescripcion;
private int Moneda;
private int Proveedor;
private String ProveedorDescripcion;
private double Saldo;
private double SaldoVencido;
private String DatosGenerales;
public void insertcliente(int Empresa,
int Cliente,
String ClienteClave,
String ClienteDescripcion,
String ClienteDatosGenerales,
int Moneda,
double Saldo,
double SaldoVencido
)
{
Object[] Data = {Empresa,Cliente,ClienteClave,ClienteDescripcion,ClienteDatosGenerales,Moneda,Saldo,SaldoVencido};
executeSQL("INSERT INTO " + DBhelper.TABLE_NAME + " VALUES(?,?,?,?,?,?,?,?)", Data );
}
public String [] [] ObtenerClientes()
{
int iCnt = 0;
String[][] Data = null;
String[] aFils = null;
Cursor aRS = querySql("SELECT * FROM " + DBhelper.TABLE_NAME , aFils);
if (aRS.getCount() > 0)
{
Data = new String[aRS.getCount()][];
while (aRS.moveToNext())
{
Data[iCnt] = new String[7];
Data[iCnt] [0]= aRS.getString(aRS.getColumnIndex(DBhelper.COLUMN_NAME_Empresa));
Data[iCnt] [1]= aRS.getString(aRS.getColumnIndex(DBhelper.COLUMN_NAME_Cliente));
Data[iCnt] [2]= aRS.getString(aRS.getColumnIndex(DBhelper.COLUMN_NAME_ClienteClave));
Data[iCnt] [3]= aRS.getString(aRS.getColumnIndex(DBhelper.COLUMN_NAME_ClienteDescripcion));
Data[iCnt] [4]= aRS.getString(aRS.getColumnIndex(DBhelper.COLUMN_NAME_ClienteDatosGenerales));
Data[iCnt] [5]= aRS.getString(aRS.getColumnIndex(DBhelper.COLUMN_NAME_Moneda));
Data[iCnt] [6]= aRS.getString(aRS.getColumnIndex(DBhelper.COLUMN_NAME_Saldo));
Data[iCnt] [7]= aRS.getString(aRS.getColumnIndex(DBhelper.COLUMN_NAME_SaldoVencido));
iCnt++;
}
}
else
{
Data = new String[0][];
}
aRS.close();
CloseDB();
return (Data);
}
public static class DBhelper extends SQLiteOpenHelper {
private static final String TAG = "DBManager";
private static final String DATABASE_NAME = "hugo.db";
private static final int DATABASE_VERSION = 15;
public static final String TABLE_NAME = "ONC_Cliente";
// public static final String _ID = "id";
public static final String COLUMN_NAME_Empresa = "Empresa";
public static final String COLUMN_NAME_Cliente = "Cliente";
public static final String COLUMN_NAME_ClienteClave = "ClienteClave";
public static final String COLUMN_NAME_ClienteDescripcion = "ClienteDescripcion";
public static final String COLUMN_NAME_ClienteDatosGenerales = "ClienteDatosGenerales";
public static final String COLUMN_NAME_Moneda = "Moneda";
public static final String COLUMN_NAME_Saldo = "Saldo";
public static final String COLUMN_NAME_SaldoVencido = "SaldoVencido";
@Override
public void onCreate(SQLiteDatabase db) {
Log.w("[CHECK]", "DBHelper.onCreate....");
db.execSQL("CREATE TABLE " + DBhelper.TABLE_NAME + "("
+ DBhelper.COLUMN_NAME_Empresa + " INTEGER ,"
+ DBhelper.COLUMN_NAME_Cliente + " INTEGER ,"
+ DBhelper.COLUMN_NAME_ClienteClave + "TEXT ,"
+ DBhelper.COLUMN_NAME_ClienteDescripcion + "TEXT ,"
+ DBhelper.COLUMN_NAME_ClienteDatosGenerales + "TEXT ,"
+ DBhelper.COLUMN_NAME_Moneda + "INTEGER ,"
+ DBhelper.COLUMN_NAME_Saldo + "DOUBLE ,"
+ DBhelper.COLUMN_NAME_SaldoVencido + "DOUBLE "
+ ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Actualizacion de BDD de la version " + oldVersion + " a la "
+ newVersion + ", de la que se destruira la informacion anterior");
db.execSQL("DROP TABLE IF EXISTS " + DBhelper.TABLE_NAME);
onCreate(db);
}
DBhelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
}
Código de adapter
public void UpdateList() {
String[][] aRef = oBD.ObtenerClientes();
if (aRef != null)
{
ArrayList<CXCPSaldoClienteProveedor> arrarcliente = new ArrayList<CXCPSaldoClienteProveedor>();
CXCPSaldoClienteProveedor cxcpSaldoClienteProveedor;
for (int iCnt = 0; iCnt < aRef.length; iCnt++){
cxcpSaldoClienteProveedor = new CXCPSaldoClienteProveedor(ONC_SYS.NullToZeroInteger(aRef[iCnt][0]),ONC_SYS.NullToZeroInteger(aRef[iCnt][1]),aRef[iCnt][2],aRef[iCnt][3],aRef[iCnt][4],ONC_SYS.NullToZeroInteger(aRef[iCnt][5]),
ONC_SYS.NullToZeroDouble(aRef[iCnt][6]),ONC_SYS.NullToZeroDouble(aRef[iCnt][7]));
arrarcliente.add(cxcpSaldoClienteProveedor);
}
ListView list = (ListView) findViewById(R.id.clientes);
adapterCliente = new AdapterCliente(Clientes.this,arrarcliente);
list.setAdapter(adapterCliente);
}
}