pwcahyo pwcahyo - 5 months ago 10
Android Question

Android Statement Sqlite Cursor

I have source code like this :

if (proses == 3) {
HistoryConnector dbkonek = new HistoryConnector(this);
dbkonek.open();

//CURSOR air
cursorSumberAir = dbkonek.getOneDataSumberAir(rowId);
cursorKegunaanAir = dbkonek.getOneDataKegunaanAir(rowId);
cursorSifatAir = dbkonek.getOneDataSifatAir(rowId);
cursorPerlakuanAir = dbkonek.getOneDataPerlakuanAir(rowId);
cursorPenyimpananAir = dbkonek.getOneDataPenyimpananAir(rowId);
cursorSanitasi = dbkonek.getOneDataSanitasi(rowId);
cursorSumberAir.moveToFirst();
cursorKegunaanAir.moveToFirst();
cursorSifatAir.moveToFirst();
cursorPerlakuanAir.moveToFirst();
cursorPenyimpananAir.moveToFirst();
cursorSanitasi.moveToFirst();
} else {
DatabaseConnector dbkonek = new DatabaseConnector(this);
dbkonek.open();

//CURSOR air
cursorSumberAir = dbkonek.getOneDataSumberAir(rowId);
cursorKegunaanAir = dbkonek.getOneDataKegunaanAir(rowId);
cursorSifatAir = dbkonek.getOneDataSifatAir(rowId);
cursorPerlakuanAir = dbkonek.getOneDataPerlakuanAir(rowId);
cursorPenyimpananAir = dbkonek.getOneDataPenyimpananAir(rowId);
cursorSanitasi = dbkonek.getOneDataSanitasi(rowId);
cursorSumberAir.moveToFirst();
cursorKegunaanAir.moveToFirst();
cursorSifatAir.moveToFirst();
cursorPerlakuanAir.moveToFirst();
cursorPenyimpananAir.moveToFirst();
cursorSanitasi.moveToFirst();
}


I want this: If
proses == 3
then
dbkonek
create new
HistoryConnector
. else
dbkonek
create new
DatabaseConnector
, how I could achieve this?
Because when I make source like this:

if (proses == 3) {
HistoryConnector dbkonek = new HistoryConnector(this);
dbkonek.open();
} else {
DatabaseConnector dbkonek = new DatabaseConnector(this);
dbkonek.open();
}
//CURSOR air
cursorSumberAir = dbkonek.getOneDataSumberAir(rowId);
cursorKegunaanAir = dbkonek.getOneDataKegunaanAir(rowId);
cursorSifatAir = dbkonek.getOneDataSifatAir(rowId);
cursorPerlakuanAir = dbkonek.getOneDataPerlakuanAir(rowId);
cursorPenyimpananAir = dbkonek.getOneDataPenyimpananAir(rowId);
cursorSanitasi = dbkonek.getOneDataSanitasi(rowId);
cursorSumberAir.moveToFirst();
cursorKegunaanAir.moveToFirst();
cursorSifatAir.moveToFirst();
cursorPerlakuanAir.moveToFirst();
cursorPenyimpananAir.moveToFirst();
cursorSanitasi.moveToFirst();


edited, any one connector like this:

public class DatabaseConnector {

private static final String DB_NAME = "DataAssessment.db";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;

public DatabaseConnector(Context context) {
dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}
}


source code above,
dbkonek
cannot detect in cursor. how i can be simple this source code.
Thanks for advance

Answer

Your class hierarchy has to be designed such that HistoryConnector and DatabaseConnector share a common base class or interface, presumably called Connector, that defines the set of operations that are available on any type of Connector. Then you would simply do:

Connector dbkonek;
if(proses == 3){
    dbkonek = new HistoryConnector(this);
}else{  
    dbkonek = new DatabaseConnector(this);
}
dbkonek.open();
...