当我单击一个 ListItem 时,它会打开一个带有 4 个 EditText 字段的自定义对话框。根据单击的行,字段设置为当前数据。该对话框的目的是允许用户更新数据(它是一个财务应用程序)。当用户在对话框中单击“提交”时,我在实际应用更新时遇到了麻烦。我运行时应用程序中没有错误。这是 onclick 方法:
protected void onListItemClick(ListView l, View v, int position, long id) {
List<Debt> values = datasource.getAllDebt();
Debt item = values.get(position);
final long boxId = item.getId();
// final String BoxId = String.valueOf(boxId);
final String BoxName = item.getName();
final String BoxBalance = item.getBalance();
final String BoxApr = item.getApr();
final String BoxPayment = item.getPayment();
// set up dialog
final Dialog dialog = new Dialog(manageDebts.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Edit Debt Details");
dialog.setCancelable(true);
// set up text
EditText et1 = (EditText) dialog.findViewById(R.id.editText1);
EditText et2 = (EditText) dialog.findViewById(R.id.editText2);
EditText et3 = (EditText) dialog.findViewById(R.id.editText3);
EditText et4 = (EditText) dialog.findViewById(R.id.editText4);
et1.setText(BoxName);
et2.setText(BoxBalance);
et3.setText(BoxApr);
et4.setText(BoxPayment);
// set up button
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
datasource.updateDebt(boxId, BoxName, BoxBalance, BoxApr,
BoxPayment);
dialog.dismiss();
}
});
dialog.show();
}
我的数据库助手类中的更新方法如下所示:
public boolean updateDebt(long updateId, String debt_name, String debt_total, String apr, String payment)
{
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_ID, updateId);
values.put(MySQLiteHelper.COLUMN_DEBT_NAME, debt_name);
values.put(MySQLiteHelper.COLUMN_DEBT_TOTAL, debt_total);
values.put(MySQLiteHelper.COLUMN_APR, apr);
values.put(MySQLiteHelper.COLUMN_PAYMENT, payment);
return database.update(MySQLiteHelper.TABLE_DEBT, values, MySQLiteHelper.COLUMN_ID + " = " + updateId, null) > 0;
}
我已验证 COLUMN_ID 和 updateId 指向 ListView 和 SQL 数据库中的正确行。
有人看到我不是的东西吗?
最佳答案
也许您的更新违反了约束条件?只是猜测,没有看到数据库代码。
编辑
去掉行 id 变量周围的单引号,这会使数据库将其视为字符串,并且将字符串与数字进行比较是失败的。
关于android - SQLite 更新不应用 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560094/