SQLite更新不应用Android
当我点击一个ListItem时,它会打开一个带有4个EditText字段的自定义对话框。根据单击的行,使用当前数据设置字段。该对话框的目的是允许用户更新数据(这是一个金融应用程序)。当用户在对话框中单击“提交”时,我在实际应用更新时遇到问题。当我运行时,应用程序中没有错误。以下是onclick方法:SQLite更新不应用Android,android,sqlite,listview,Android,Sqlite,Listview,当我点击一个ListItem时,它会打开一个带有4个EditText字段的自定义对话框。根据单击的行,使用当前数据设置字段。该对话框的目的是允许用户更新数据(这是一个金融应用程序)。当用户在对话框中单击“提交”时,我在实际应用更新时遇到问题。当我运行时,应用程序中没有错误。以下是onclick方法: protected void onListItemClick(ListView l, View v, int position, long id) { List<Debt> v
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();
}
我已经验证了列ID和updateId是否指向ListView和SQL数据库中的正确行
有人看到我没有看到的东西吗?我在我的电脑里测试你的代码 更新方法工作正常 所以我认为你应该发布更多的代码 或者您应该检查您的逻辑。这将更好地工作:
String whereClause = MySQLiteHelper.COLUMN_ID + " = ?";
String[] whereArgs = new String[]{ String.valueOf(updateId) };
return database.update(MySQLiteHelper.TABLE_DEBT,
values, whereClause, whereArgs) > 0;
String.valueOf()
调用只是将ID转换为字符串值。您的更新是否违反了约束?只是一个猜测,没有看到数据库代码
编辑
丢失row id变量周围的单引号,这使DB将其视为字符串,将字符串与数字进行比较是失败的。我在2和3之间缺少一个步骤 再次设置变量,在onClick方法内部。用户按下更新按钮后,必须将变量重新设置为EditText字段的新值。像这样(简化版):
谢谢你的评论。。。它简化了代码。。但并没有解决这个问题。在
updateDBT
方法中,数据库的值来自哪里?它是一个可写数据库吗?或者你调用了getReadableDatabase
?我刚刚在原始帖子中添加了这个类的所有内容。再次感谢你的帮助!我确认数据库是可写的。在上面的最后一段代码中,我没有看到您在DebtDataSource
类上调用open()
方法。也许这就是问题所在?不确定这是否是您需要的,但我发布了更多的DB代码。如果没有,违反约束会做什么?导致更新失败。您的所有字段似乎都是text not null
,因此我打赌您试图将null保存到其中一个字段会导致更新失败。您可以尝试使用updateWithOnConflict
并告诉它在违反约束时该怎么办。使用祝酒,我确认进入update方法的所有变量都不是null。最后一个想法是,行id正确吗?编辑了我的答案,请查看。发布您的数据源。getAllDebt();我刚刚添加了我的整个DebtDataSource类,它包含了我的大部分SQL代码。
String whereClause = MySQLiteHelper.COLUMN_ID + " = ?";
String[] whereArgs = new String[]{ String.valueOf(updateId) };
return database.update(MySQLiteHelper.TABLE_DEBT,
values, whereClause, whereArgs) > 0;
String name = null;
EditText et;
name = debt.getName();
et.setText(name);
onclick {
***name = et.getText().toString();***
datasource.updateDebt(name);
}