我的 db4o 数据库没有更新。在我的 ViewContact 类中,我有一个按钮,按下该按钮会询问用户输入,并将其存储在数据库中。它不存储任何想法?
我在 ViewContact 类下面发布了问题代码,
@SuppressLint({ "ParserError", "ParserError", "ParserError", "ParserError" })
public void onClick(View v) {
switch(v.getId())
{
/*THIS IS THE ADD PHRASE BUTTON*/
case R.id.bAddP:
/*ALERT BOX LAUNCHES*/
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
/* Set an EditText view to get user input */
final EditText input = new EditText(this);
alert.setView(input);
/*THIS IS FOR WHEN OK IS PRESSED*/
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/*VALUE OF USER INPUT*/
String value = input.getText().toString();
/* OPEN DB4O AND UPDATE CONTACT OBJECT*/
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
.newConfiguration(), db4oDBFullPathSdCard(context));
try {
/*QUERY BY EXAMPLE TO GET CONTACT
USING "t" WHICH IS PARCELABLE OBJECT PASSED FROM
FIRST ACTIVITY*/
ObjectSet result = db.queryByExample(new NewContact(t.getName()));
NewContact found = (NewContact) result.next();
/*storePhrase STORES A STRING INTO THE OBJECTS
ARRAYLIST*/
found.storePhrase(value);
/*UPDATE DATABASE,
THIS IS NOT UPDATING! HERE IS PROBLEM*/
db.store(found);
Class ourContact = Class.forName("com.text.q.ViewContacts");
Intent i = new Intent(ViewContact.this, ourContact);
//REPARCEL OBJECT SO NAME CAN BE USED AGAIN
i.putExtra("New", t);
finish();
startActivity(i);
/*REFRESH ACTIVITY*/
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
db.close();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
break;
}
}
问题在于 bAddP 的 onClick 方法。我正在尝试更新数据库,以便当我结束并重新启动活动时,将显示新短语。“db.store(found);”行 应该更新我的数据库,但它没有。我意识到这不是完美的代码,因此任何想法都将不胜感激。谢谢