0

When I click on a ListItem, it opens up a custom dialog with 4 EditText fields. The fields are set with current data depending on the row that is clicked. The purpose of the dialog is to allow the user to update the data (it is a financial app). I am having trouble actually applying the update when the user clicks "submit" in the dialog. There are no errors in the app when I run. Here is the onclick method:

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();
}

The Update Method in my Database helper class is shown here:

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;
}

I have verified that the COLUMN_ID and the updateId are pointing to the correct rows in the ListView and the SQL database.

Does someone see something I am not?

4

4 回答 4

1

也许您的更新违反了约束?只是猜测,没有看到数据库代码。

编辑

丢失行 id 变量周围的单引号,这使得数据库将其视为字符串,并且将字符串与数字进行比较是失败的。

于 2012-05-12T00:45:33.247 回答
0

I test your code in my computer.

The update method works fine.

So I think you should post more code .

or You should check your logic.

于 2012-05-12T00:32:54.090 回答
0

这将更好地工作:

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 转换为字符串值。

于 2012-05-12T00:43:21.210 回答
0

我错过了 2 到 3 之间的一步。

onClick 方法中再次设置变量。 一旦用户按下更新按钮,该变量必须重新设置为 EditText 字段的值。像这样(简化版):

 String name = null;
 EditText et;

 name = debt.getName();
 et.setText(name);
 onclick {
     ***name = et.getText().toString();***
     datasource.updateDebt(name);
 }
于 2012-05-24T16:28:08.067 回答