English 中文(简体)
Android 的 SQLite 更新方法
原标题:SQLite Update Method for Android

我的问题是,你如何称呼这种方法。我已经知道如何实施。这是方法的内容:

 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;

我的问题在于“强烈”调用 这个更新的Debt方法。

使用更新按钮绘制四个字段的对话框。 字段预先配有表格数据。 用户更改字段数据和点击更新, 表格应该更改 。

这是呼叫前的代码 。 记住, 我通过点击在 ListTroundClick 的对话框中 呼叫它 。

(我编辑了很多不相干的东西出来)

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....

最佳回答

我终于想出答案了 这就是我正在做的:

  1. Creating a String variable for each column in a specific row of debt.
  2. Pre-populating the fields in the dialog with that variable.
  3. Then passing the same variables into the update method

例如(简化):

 String name = null;
 EditText et;

 name = debt.getName();
 et.setText(name);

 onclick {
     datasource.updateDebt(name);
 }

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:

 String name = null;
 EditText et;

 name = debt.getName();
 et.setText(name);
 onclick {
     ***name = et.getText().toString();***
     datasource.updateDebt(name);
 }
问题回答

尝试此 :

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);
    return mDb.update(MySQLiteHelper.TABLE_DEBT, values, MySQLiteHelper.COLUMN_ID + "=" + updateId, null) > 0;

检查返回的布尔值 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签