我有ListView
中从服务器获取的项目列表。我的问题是:如何将listview中显示的项目列表存储到Android Sqlite本地数据库?
答案 0 :(得分:2)
尝试以下代码 -
首先,您必须创建DatabaseHandler
类
DatabaseHandler.java -
package com.androidhive.androidsqlite;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends 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 DatabaseHandler(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("vlaue 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;
}
}
MainActivity.java
package com.androidhive.androidsqlite;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
ArrayList<String> your_list_arrayArrayList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
your_list_array ArrayList = new ArrayList<String>();
your_list_arrayArrayList.add("Item1");
your_list_arrayArrayList.add("Item2");
your_list_arrayArrayList.add("Item3");
your_list_arrayArrayList.add("Item4");
your_list_arrayArrayList.add("Item5");
DatabaseHandler db = new DatabaseHandler(this);
db.addListItem(your_list_arrayArrayList);
Cursor cursor = db.getListItem();
Log.e("count", " " + cursor.getCount());
if (cursor != null) {
cursor.moveToNext();
do {
Log.e("value==", "" + cursor.getString(1));
} while (cursor.moveToNext());
}
}
}
如果它无法正常工作,请告诉我,我会尽力帮助您。
答案 1 :(得分:0)
只需获取所有项目列表并将其存储到SQLite数据库中。要将数据存储到SQLite数据库,请转到here