Android 使用列表处理来自数据库的更新
我在使用列表处理数据库更新时遇到了一个问题,在编辑类中我有这个方法Android 使用列表处理来自数据库的更新,android,sqlite,sql-update,Android,Sqlite,Sql Update,我在使用列表处理数据库更新时遇到了一个问题,在编辑类中我有这个方法 private ArrayList<store> store_List = new ArrayList<store>(); list = (ListView) findViewById(R.id.list); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(
private ArrayList<store> store_List = new ArrayList<store>();
list = (ListView) findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
store store = store_List.get(position);
Intent intent = new Intent(edit.this,Update.class);
edit.this.finish();
startActivity(intent);
}
});
在DB中,方法更新如下:
public int updatestore(store s) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, s.getName());
values.put(KEY_CATEGORY, s.getCategory());
return db.update(TABLE_store, values, KEY_ID + " = ?",
new String[] { String.valueOf(s.getEid()) });
}
您可以将从store_列表中选择的store项传递到Update.class,如下所示:
list = (ListView) findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
store store = store_List.get(position);
Intent intent = new Intent(edit.this,Update.class);
intent.putExtra("myStore", store);
edit.this.finish();
startActivity(intent);
}
});
Store passedStore = getIntent().getSerializableExtra("myStore");
然后在OnClick Listener中,您将拥有以下内容:
btnUpdate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DB db = new DB(getApplicationContext());
try {
db.open();
db.updatestore(passedStore); //the store item you became as an extra from your intent
adapter.notifyDataSetChanged();
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(),
"Could not create or Open the database");
} finally {
if (db != null)
db.close();
}
}
});
你会变成什么样的错误?我没有错误,我只是想知道我应该在db.updatestoreHERE中传递什么;是的,我看到了,等一下,我会给你写我的建议作为回答你的updatestore方法接受一个存储变量。因此,您必须首先在那里传递一个存储变量。更新数据库后,从数据库获取所有数据并将这些值传递给适配器,然后调用yourAdapter.notifyDataSetChanged;非常感谢。当我写入Store passedStore=getIntent.getSerializableExtramyStore时;告诉您将强制转换添加到存储区或将passedScore的类型更改为可序列化时出错。我应该怎么做?在你的Store类中实现Serilizable.try类型转换Store passedStore=StoregetIntent.getSerializableExtramyStore;05-23 13:46:59.979:E/AndroidRuntime20754:java.lang.RuntimeException:无法实例化活动组件{com.example.storesystem/com.example.storesystem.Update}:java.lang.NullPointerException 05-23 13:46:59.979:E/AndroidRuntime20754:at android.app.ActivityThread.performLaunchActivityActivityThread.java:1573 05-23 13:46:59.979:E/AndroidRuntime20754:at android.app.ActivityThread.HandleLaunchActivityThread.java:1667
btnUpdate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DB db = new DB(getApplicationContext());
try {
db.open();
db.updatestore(passedStore); //the store item you became as an extra from your intent
adapter.notifyDataSetChanged();
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(),
"Could not create or Open the database");
} finally {
if (db != null)
db.close();
}
}
});