尽管返回1,SQLite数据库仍无法更新行

时间:2016-11-03 12:50:08

标签: android sql sqlite listview sqliteopenhelper

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

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

我缺少什么?

1 个答案:

答案 0 :(得分:0)

很难清楚地说明您向我们展示的代码不完整,但如果您正在使用database.beginTransaction();,请确保在更新通话后使用database.setTransactionSuccessful();。另外,请勿在{{1​​}}声明中忘记database.endTransaction();