-1

我有这样的源代码:

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

我想要这个:如果proses == 3然后dbkonek创建新的HistoryConnector. 否则dbkonek创建新DatabaseConnector的,我怎么能做到这一点?因为当我制作这样的来源时:

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

编辑,任何一个这样的连接器:

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

上面的源代码,dbkonek无法在游标中检测到。我怎么能简单这个源代码。感谢提前

4

1 回答 1

1

您的类层次结构必须设计为HistoryConnector共享DatabaseConnector一个通用的基类接口(大概称为),它定义了在任何类型的连接器Connector上可用的操作集。然后,您只需执行以下操作:

Connector dbkonek;
if(proses == 3){
    dbkonek = new HistoryConnector(this);
}else{  
    dbkonek = new DatabaseConnector(this);
}
dbkonek.open();
...
于 2012-12-26T04:31:27.970 回答