希望有人或許能夠對此有所幫助,以幫助我。我有一個EditText字段,用戶將輸入序列號和或參考號碼,然後點擊加載按鈕。Android SQLite:入門驗證/檢查
http://i.stack.imgur.com/95iWI.png
我做了一個方法來檢查,看看是否這個數字已添加到數據庫中。如果沒有,那麼我會彈出一個Yes/No對話框,詢問他們是否要添加到數據庫中。然後,如果它在數據庫中,它會將該數字的數據加載到四個TextView中。
http://i.stack.imgur.com/dHbCQ.png
如果選擇是,那麼它運行的新意圖,切換到表格屏幕(尚未看着傳遞用戶輸入到其他類還)。但要回到我的小問題,在模擬器中測試時,一切運行良好。然後當我輸入一個我已經輸入的數字時,仍然會打開對話框(我在if語句中)。
Load按鈕代碼
String s = input.getText().toString();
int l = Integer.parseInt(s);
Db load = new Db(this);
load.open();
y = load.dbCheck(l);
if (y != 0) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Intent i = new Intent("android.intent.action.FORM");
startActivity(i);
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Would you like to add to Database?")
.setTitle("Not in Database:")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener)
.show();
}
else
{
final Dialog dbentery = new Dialog(this);
dbentery.setTitle("Entry Exists!");
TextView tv = new TextView(this);
tv.setText("The serial number you have entered is already in database");
dbentery.setContentView(tv);
dbentery.show();
final Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
dbentery.dismiss();
t.cancel();
}
}, 2000);
}
load.close();
現在與方法我創建我原本以爲是正確的,但有些事情是行不通的。我有它返回一個整數回到加載按鈕代碼,其中if語句使用枯萎0或1來驗證它是否在數據庫中,以及它應該做什麼。我想,因爲它正在運行對話框,它正在返回0,它可能與光標。我看了一些其他類似的帖子,但似乎沒有幫助我,或者我不理解正在做什麼。因此,這裏是我的DBHelper類的檢查方法
/* this is to check to see if entry is in db or not */
public int dbCheck(int l) throws SQLException {
String[] cols = new String[] { KEY_ROWID, KEY_ONE, KEY_TWO, KEY_THREE, KEY_FOUR };
Cursor c = ourDB.query(KEY_TABLE, cols, KEY_ONE + "=" + l, null,
null, null, null);
if (c == null) {
return 0;
}else {
return 1;
}
}
這可能是一個簡單的修復,但我沒有看到它...感謝任何幫助或洞察力,我可能是做錯了
你不覺得,如果你彈出一個對話框,你已經互換了東西?我的意思是,你應該檢查,如果(l == 0){//數字不存在於db中} else {// number exists}你做了反向...不是嗎? – Hiral
ahh我完全忘記了改變它,謝謝指出 – acrichm
(你應該在退出你的'#dbCheck(int)'方法之前調用'Cursor#close()') – Jens