我有一个用db行数据填充的ListView。然后我打开一个与用于创建/新行的原始页面相同的intent。我想重新使用它来编辑/更新。我试图将整个行数据集合到新的编辑活动中,以便可以对其进行操作,然后更新行。请注意,没有使用ListActivities,我不打算去那条路线。计划中还有一个删除和取消,但我现在很高兴让更新工作。
插入类具有:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==submitButton){
String name = nameEditText.getText().toString();
String location = locationEditText.getText().toString();
String notes = notesEditText.getText().toString();
long insert = db.insertRecord(name, location, retn, nh, busy, child, man, woman, notes);
Toast.makeText(getApplicationContext(), "Record inserted successfully", Toast.LENGTH_SHORT).show();
db.close();
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}else if(v==cancelButton){
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}else yada, yada, yada...
具有ListView的类:
public class ViewRecords extends Activity implements OnItemLongClickListener, OnClickListener {
private static final String TAG = null;
int position;
Dialog updateDialog;
EditText updatename,updatelocation;
Button updateButton,cancelButton;
DBAdapter db;
Cursor c;
ListView list;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
db = new DBAdapter(getApplicationContext());
db.openDatabase();
list = (ListView)findViewById(R.id.listView1);
updateListView();
list.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> list, View customlistView,
int position, long id) {
Toast.makeText(getApplicationContext(),
"ListItem Number " + position + ". id: " + id, Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(ViewRecords.this, Edit.class);
ViewRecords.this.startActivity(myIntent);
return true;
}
});
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return c.getCount();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
...和编辑类:
public class Edit extends Activity{ // implements OnClickListener{
EditText nameEditText,locationEditText, notesEditText;
DBAdapter db;
CheckBox cb1, cb2, cb3, cb4, cb5, cb6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
那么,任何人都可以建议我如何从这里到达那里吗? 谢谢!
编辑:下面的适配器。
public class DBAdapter {
// database name
public static final String DATABASE_NAME = "UserDB";
// table name
public static final String TABLE_NAME = "tableone";
// database version
public static final int DATABASE_VERSION = 1;
// defining columns
public static final String COL_ROWID = "rowid";
public static final String COL_NAME = "name";
public static final String COL_LOCATION = "location";
public static final String COL_RETURN = "retn";
public static final String COL_NH = "nh";
public static final String COL_BUSY = "busy";
public static final String COL_CHILD = "child";
public static final String COL_MAN = "man";
public static final String COL_WOMAN = "woman";
public static final String COL_NOTES = "notes";
String CREATE_TABLE = "create table tableone(rowid integer primary key autoincrement,name text,location text not null, retn text, nh text, busy text, child text, man text, woman text, notes text)";
Context context;
DBHelper dbHelper;
SQLiteDatabase db;
public DBAdapter(Context c) {
// TODO Auto-generated constructor stub
this.context = c;
dbHelper = new DBHelper(context);
}
DBAdapter openDatabase() {
db = dbHelper.getWritableDatabase();
return this;
}
void close() {
dbHelper.close();
}
long insertRecord(String name, String location, String retn, String nh, String busy, String child, String man, String woman, String notes) {
ContentValues con = new ContentValues();
con.put(COL_NAME, name);
con.put(COL_LOCATION, location);
con.put(COL_RETURN, retn);
con.put(COL_NH, nh);
con.put(COL_BUSY, busy);
con.put(COL_CHILD, child);
con.put(COL_MAN, man);
con.put(COL_WOMAN, woman);
con.put(COL_NOTES, notes);
return db.insert(TABLE_NAME, null, con);
}
Cursor getAllRecords(){
String[] columns = {COL_ROWID,COL_NAME,COL_LOCATION, COL_RETURN, COL_NH, COL_BUSY, COL_CHILD, COL_MAN, COL_WOMAN, COL_NOTES};
return db.query(TABLE_NAME, columns, null, null, null, null, null);
}
void deleteAllRecords(){
db.delete(TABLE_NAME, null, null);
}
void deleteOneRecord(String rowid){
db.delete(TABLE_NAME, rowid +"="+COL_ROWID, null);
}
long updateRecord(String rowid,String name,String location, String retn, String nh, String busy, String child, String man, String woman, String notes){
ContentValues con = new ContentValues();
con.put(COL_NAME, name);
con.put(COL_LOCATION, location);
con.put(COL_NH, nh);
con.put(COL_RETURN, retn);
con.put(COL_BUSY, busy);
con.put(COL_CHILD, child);
con.put(COL_MAN, man);
con.put(COL_WOMAN, woman);
con.put(COL_NOTES, notes);
return db.update(TABLE_NAME, con, rowid +"="+COL_ROWID, null);
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
答案 0 :(得分:0)
而不是从行中获取整个数据,更好的解决方案是获取被单击的行的id然后将您的意图中的包中的id传递给您的活动,然后从您的{中的意图中提取id {1}}活动并从具有该ID的该行的数据库中提取所有内容。
你没有真正展示你如何使用你的适配器,所以我真的不能告诉你更多,但如果你想传递整个数据,你要么必须传递你想要发送的每个值分开; y在意图中或创建一个保存数据并使该类扩展Edit
并将该类配置为Parcelable对象的对象