我正在开发一个小应用程序,用于记录他们所属的地点和类别。现在,我只是在处理类别表,一旦我设法执行所有CRUD操作,那么我将进入应用程序的其余部分。
问题是,我设法插入,删除和读取该表但我无法更新。我有这个代码:
将对DBAdapter:
...
public boolean updateCategory(long rowId, String name) {
ContentValues args = new ContentValues();
args.put(KEY_ID_CAT, rowId);
args.put(KEY_NAME_CAT, name);
return db.update(TABLE_CATEGORY, args, KEY_ROWID_CAT + "=" + rowId, null) > 0;
}
...
我的EditCategory类:
public class S7_EditCategory extends Activity {
EditText et_name_category;
private Context ctx;
int duration = Toast.LENGTH_SHORT;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.s7_edit_categories);
et_name_category = (EditText) findViewById(R.id.edit_category);
ctx = this;
final String nome = getIntent().getStringExtra("nome");
final long id_cat = getIntent().getIntExtra("_id", 0);
et_nome_category.setText(nome);
Button button_save = (Button) findViewById(R.id.button3);
button_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
String new_cat = et_name_category.getText().toString();
DBAdapter dbadap = DBAdapter.getInstance(ctx);
dbadap.updateCategory(id_cat, new_cat);
Intent i = new Intent(ctx, S5_Categories.class);
setResult(RESULT_OK, i);
finish();
}
catch (Exception ex) {
throw new Error("some error");
}
}
});
}
}
Logcat告诉我(_id = 0)所以,我认为它与上一个活动传递的参数有关,分类:
public class S5_Categories extends ListActivity {
private Context ctx;
private SimpleCursorAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.s5_categorias);
ctx = this;
fillList();
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parentView, View view, int position, long id) {
Cursor c = (Cursor) parentView.getItemAtPosition(position);
Intent i = new Intent(ctx, S7_EditCategoria.class);
i.putExtra("nome", c.getString(c.getColumnIndexOrThrow("nome")));
i.putExtra("id_cat", c.getColumnIndex("_id") + 1);
startActivityForResult(i, 0);
}
});
}
private void fillList() {
try {
DBAdapter dbadap = DBAdapter.getInstance(ctx);
Cursor c = dbadap.getAllCategorias();
String[] from = new String[] { "nome" };
int[] to = new int[] { android.R.id.text1 };
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, c, from, to,
SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setListAdapter(adapter);
}
catch (Exception ex) {
throw new Error(ex.getMessage());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if (data != null) {
DBAdapter dbadap = DBAdapter.getInstance(ctx);
Cursor cursor = dbadap.getAllCategorias();
adapter.changeCursor(cursor);
}
}
}
}
好吧,有人可以帮帮我吗?
提前致谢
Chiapa
好的,现在我设法得到了那个参数id,但是在更新时我仍然会遇到错误:我尝试时会遇到如下错误:
错误更新名称=医院id_cat = 4使用UPDATE类别SET name =?,id_cat =? WHERE id = 4
请帮助
Chiapa
答案 0 :(得分:1)
尝试以下方式
db.update(TABLE_CATEGORY, args, KEY_ROWID_CAT + "=?", new String[] { (rowId) });
而不是
db.update(TABLE_CATEGORY, args, KEY_ROWID_CAT + "=" + rowId, null)
为什么要将它作为int
。他们是否有正当理由。我没有从你提供的代码中找到任何理由。如果它们没有正当理由,则意味着将方法更改为如下所示。
public boolean updateCategory(long rowId, String name) {
ContentValues args = new ContentValues();
args.put(KEY_ID_CAT, rowId);
args.put(KEY_NAME_CAT, name);
db.update(TABLE_CATEGORY, args, KEY_ROWID_CAT + "=?", new String[] { (rowId) });
}
我希望这会对你有所帮助。