我在一个活动中有一个数据库,我想在另一个活动中打开,在这个活动中我也希望创建一个列表视图:
我的代码如下
messagedb=context.openOrCreateDatabase("message",0, null);
messagedb.execSQL(
"CREATE TABLE IF NOT EXISTS tab( " +
" _id INTEGER PRIMARY KEY AUTOINCREMENT ," +
" nickname varchar,sender INT(13),body varchar)");
messagedb.execSQL("INSERT INTO tab VALUES('"+nickname+"','"+sender+"','"+sb+"')");
messagedb.close();
mydb.close();
当我插入值时,它说您的数据库有 4 列,但您输入了 3 列值......我怎样才能从表中自动添加 _id......
我的 listView 活动如下
public class ListView extends ListActivity {
SQLiteDatabase messagedb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_list);
messagedb=ListView.this.openOrCreateDatabase("message",0, null);
messagedb.execSQL(
"CREATE TABLE IF NOT EXISTS tab(" +
" _id INTEGER PRIMARY KEY AUTOINCREMENT," +
" nickname varchar,sender INT(13),body varchar)");
Cursor cur = messagedb.rawQuery("select _id, nickname, body from tab", null);
String[] from = new String[] { "nickname", "body" };
int[] to = new int[] { R.id.sender_entry, R.id.body_entry};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.my_list_entry, cur, from, to);
this.setListAdapter(mAdapter);
cur.close();
messagedb.close();
}
protected void onListItemClick(android.widget.ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String nickName = ((TextView) v.findViewById(R.id.sender_entry)).getText().toString();
String body = ((TextView) v.findViewById(R.id.body_entry)).getText().toString();
Intent intent = new Intent(ListView.this,MessageViewPage.class);
Bundle b = new Bundle();
b.putString("nick", nickName);
b.putString("body", body);
intent.putExtras(b);
startActivity(intent);
finish();
}
}