最近关于我的数据库驱动的 android 应用程序,我遇到了一个奇怪的问题。我之前使用代码 UPDATE 没有问题,但现在它不允许我更新对记录的更改。
如果它保存了我所做的更改,我确实尝试记录,但它没有。
这是我正在使用的代码:
private void initControls() {
userInput = (EditText) findViewById (R.id.editTextDialogUserInput);
save = (Button) findViewById (R.id.btSave);
cancel = (Button) findViewById (R.id.btCancel);
save.setOnClickListener(this);
cancel.setOnClickListener(this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
stID = extras.getString("dog_id");
dog_name = extras.getString("dog_name");
cursor = dbHelper.fetchbBreedByName(dog_name);
strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
Log.d("Animal ID", "Animal ID is " + stID + " and breed is " + dog_name);
userInput.setText(strDesc);
}
}
private void checkDatabaseConnection() {
// TODO Auto-generated method stub
dbHelper = new DBHelper(this);
try {
dbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
dbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btSave:
if(userInput.equals("")){
Toast.makeText(this, "No input", Toast.LENGTH_SHORT).show();
}
else {
id = Long.valueOf(stID);
dbHelper.updateDescription( id, userInput.getText().toString() );
Toast.makeText(this, "Description has been updated successfully!",
Toast.LENGTH_SHORT).show();
strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
Log.d("Updated", dog_name + " " + strDesc);
Intent i = new Intent(this, DogClass.class);
startActivity(i);
finish();
}
break;
case R.id.btCancel:
userInput.setText("");
break;
}
}
@Override
protected void onDestroy() {
dbHelper.close(); // close DB
cursor.close(); // close cursor
super.onDestroy();
}
我真的不知道怎么了,这里有没有人遇到过这种情况?不妨帮助我弄清楚我的代码中缺少什么。非常感谢您的帮助。谢谢。