【问题标题】:SQLite Database fails to update row despite returning 1尽管返回 1,SQLite 数据库仍无法更新行
【发布时间】:2017-03-17 01:33:44
【问题描述】:

我显示一个带有数据库行的列表视图。单击一行时,会弹出一个对话框,其中包含该行的(可编辑)项目。

public class ViewDataActivity extends ListActivity {

private ListView listView;

HashMap<Integer, String> results = new HashMap<Integer, String>();;
ArrayList<String> dataValues = new ArrayList<String>();
ArrayList<Integer> keyValues = new ArrayList<Integer>();

ArrayAdapter<String> arrayAdapter;

Integer itemKey;
String itemValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    results = (HashMap<Integer, String>) getIntent().getSerializableExtra(MainActivity.EXTRA_MESSAGE);

    for(Map.Entry<Integer, String> entry: results.entrySet()) {
        keyValues.add(entry.getKey());
        dataValues.add(entry.getValue());
    }

    // Display items
    listView = getListView();
    View v = getLayoutInflater().inflate(R.layout.list_view_header, null);
    listView.addHeaderView(v);

    arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            dataValues );

    listView.setAdapter(arrayAdapter);

    // Make Items clickable
    listView.setChoiceMode(CHOICE_MODE_SINGLE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Log.d("List position clicked", String.valueOf(listView.getCheckedItemPosition()));
            itemKey = keyValues.get(listView.getCheckedItemPosition()-1);
            itemValue = dataValues.get(listView.getCheckedItemPosition()-1);
            showInputBox(itemKey, itemValue);
        }
    });

}

// Show dialog with values
public void showInputBox(final Integer itemKey, String listItemValue){
    final Dialog dialog=new Dialog(ViewDataActivity.this);
    dialog.setTitle("Edit Item");
    dialog.setContentView(R.layout.list_item_edit_popup);
    TextView txtMessage=(TextView)dialog.findViewById(R.id.txtmessage);
    txtMessage.setText("Update");
    txtMessage.setTextColor(Color.parseColor("#ff2222"));

    String[] listItemValues = listItemValue.split(",");

    final String liCategory = listItemValues[2];
    final String liDescription = listItemValues[3];
    final String liAmount = listItemValues[4];

    EditText editText1=(EditText)dialog.findViewById(R.id.txtinput1);
    EditText editText2=(EditText)dialog.findViewById(R.id.txtinput2);
    EditText editText3=(EditText)dialog.findViewById(R.id.txtinput3);

    editText1.setText(liCategory);
    editText2.setText(liDescription);
    editText3.setText(liAmount);

    Button bt=(Button)dialog.findViewById(R.id.btdone);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Write new Values
            dbHelper.updateExpense(itemKey, liCategory, liDescription, liAmount);
            // Refresh ListView
            arrayAdapter.notifyDataSetChanged();
            dialog.dismiss();
        }
    });
    dialog.show();
}

}

但是当我更新对话框中的值并单击按钮时,对话框关闭但数据库中的行没有更新。正在执行的更新是这样的:

public boolean updateExpense(Integer id, String category, String description, String amount) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_CATEGORY, category);
    contentValues.put(COLUMN_DESCRIPTION, description);
    contentValues.put(COLUMN_AMOUNT, amount);
// Returns 1 but no update    
Log.d("DBUpdate", String.valueOf(db.update(TABLE_NAME, contentValues, COLUMN_ID + " = ? ", new String[] { Integer.toString(id) } )));
    return true;
}

我错过了什么?

【问题讨论】:

  • 为什么不用CursorLoader更新ListView而不重新查询数据库?
  • 我缺少什么?您正在检查的代码是否有没有更新
  • @Selvin 你能详细说明一下吗?用于填充列表视图的相同代码可用于验证行是否已更新。
  • 这个问题被否决的任何原因?

标签: android sql sqlite listview sqliteopenhelper


【解决方案1】:

很难说清楚您向我们展示的代码不完整,但如果您使用的是database.beginTransaction();,请确保在更新调用后使用database.setTransactionSuccessful();。也不要忘记 database.endTransaction(); 在您的 finally 声明中。

【讨论】:

  • 这是显示相关对话框和数据库更新操作的整个代码。如果您需要任何具体信息,请告诉我
  • 好的,那么也许可以尝试用上述方法包装你的电话。可能有帮助..
  • 当你说数据库没有更新时——你怎么知道的?您是否从手机中取出数据库并检查过?
  • 是的,我重新加载了应用程序和列表视图,并注意到该行是相同的。我会试试你说的方法
  • vkislins,不幸的是仍然没有更新:(
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 2016-04-06
  • 2012-04-04
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多