我有一个listview,我会将他的全部数据保存在sqLite数据库中。我已经得到了帮助,但我无法通过一个简单的按钮将数据保存在数据库中;只需点击"保存列表"按钮应该保存整个列表,只是为了看,可能在LogCat中检索结果..这是帮手
`public class SqlHelper扩展了SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "MyDatabase";
// Database table name
private static final String TABLE_LIST = "MyListItem";
// Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_ListItem = "listitem";
public SqlHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LIST_TABLE = "CREATE TABLE " + TABLE_LIST + "(" + KEY_ID
+ " INTEGER," + KEY_ListItem + " TEXT" + ")";
db.execSQL(CREATE_LIST_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIST);
// Create tables again
onCreate(db);
}
void addListItem(ArrayList<String> listItem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
for (int i = 0; i < listItem.size(); i++) {
Log.e("value inserting==", "" + listItem.get(i));
values.put(KEY_ListItem, listItem.get(i));
db.insert(TABLE_LIST, null, values);
}
db.close(); // Closing database connection
}
Cursor getListItem() {
String selectQuery = "SELECT * FROM " + TABLE_LIST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}
}`
我尝试在onClick中编写db.addListItem(titles);
例如..标题是:ArrayList<String> titles;
但它崩溃了......我认为它不正确。有什么帮助吗?