我正在开发一个小应用程序来记录地点和它们所属的类别。现在,我只是在处理类别表,一旦我设法执行所有 CRUD 操作,我就会进入应用程序的其余部分。
问题是,我设法从该表中插入、删除和读取,但我无法更新。我有这个代码:
数据库适配器:
...
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);
}
}
}
}
好吧,有人可以帮我吗?提前感谢恰帕
好的,现在我设法获得了该参数 id,但在更新时仍然出现错误:当我尝试时,我收到如下错误:
使用 UPDATE 类别 SET name=?, id_cat=? 更新 name=hospitals id_cat=4 时出错 哪里 id=4
请帮忙
恰帕