My question is how to you call a method like this. I already know how to implement it. Here is the method content:
public boolean updateDebt(long updateId, String debtName, String debtTotal,
String debtApr, String paymentGoal) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_DEBT_NAME, debtName);
values.put(MySQLiteHelper.COLUMN_DEBT_TOTAL, debtTotal);
values.put(MySQLiteHelper.COLUMN_APR, debtApr);
values.put(MySQLiteHelper.COLUMN_PAYMENT, paymentGoal);
String whereClause = MySQLiteHelper.COLUMN_ID + " = ?";
String[] whereArgs = new String[]{ String.valueOf(updateId) };
return database.update(MySQLiteHelper.TABLE_DEBT, values, whereClause, whereArgs) > 0;
My questions lies in calling this updateDebt Method.
Picture a dialog with four fields with an update button. The fields are pre-populated with table data. The user changes field data and hits update, the table should change.
This is the code that comes before the call. Keep in mind, I am calling it via an onClick inside a dialog inside an onListItemClick.
(I’ve edit lots of unrelated stuff out)
List<Debt> values;
MyArrayAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debts);
datasource = new DebtDataSource(this);
datasource.open();
values = datasource.getAllDebt();
adapter = new MyArrayAdapter(this, values);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, final int position, long id) {
Debt item = values.get(position);
final long boxId = item.getId();
final String BoxName = item.getName();
final String BoxBalance = item.getBalance();
final String BoxApr = item.getApr();
final String BoxPayment = item.getPayment();
// (edited out: setting up EditText fields here with data from above)
// set up button
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
datasource.updateDebt(Long.valueOf(boxId), BoxName, BoxBalance,
BoxApr, BoxPayment);
values = datasource.getAllDebt();
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
dialog.show();
}
Simply put nothing happens when I hit update. The dialog dismisses, that’ it. Nothing in logcat. I have logged out the variables just before the update method inside the updateDebt() method. I have toasted them back and all the variables match up perfectly. The database doesn’t change though!
Struggling on this for days….
I finally figured out the answer. This is what I WAS doing:
For example (simplified):
I was missing a step between 2 and 3.
Setting the variable again, inside the onClick method. Once the user pressed the update button, the variable had to be RE-SET to the new value of the EditText field. Like this: