Shared Prefrences 參數共用
儲存位置:data/data/包名/shared_prefs/MainAcitivy.xml
格式:xml
儲存資料:
//擷取Shared Prefrences類型對象
SharedPrefrences sp = getSharedPrefrences("xxx",0);
或者
SharedPrefrences sp = getPrefrences(0);
//擷取Edit類型的對象
Edit edit = sp.edit();
//寫入資料
edit.putString("name","小明");
edit.putBoolean("isTrue","true");
//提交資料(必須要有)
edit.commit();
提取資料
//擷取SharedPrefrences類型對象
SharedPrefrences sp = getSharedPrefrences("xxx",0);
或者
SharedPrefrences sp = getPrefrences(0);
//根據存入的key值取出相應的資料
String name = sp.getString("name",小紅);
boolean b = sp.getBoolean("isTrue",false);
SQLite DataBase 資料庫儲存
Android平台整合小型資料庫SQLite
特點:跨平台、免費、輕量級、多線程
SQL語句:
與表相關:
a. 建表:CREATE TABLE 表名 (id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))
b. 刪表:DROP TABLE 表名
c. 改表:ALERT TABLE 表名 ADD age INTEGER
與資料相關:增、刪、改、查
a. 增加資料:INSERT INTO 表名 (欄位名) VALUES (值)
b. 刪除資料:DELETE FROM 表名 WHERE 條件
c. 修改資料:UPDATE 表名 SET 欄位名=值 WHERE 條件
d. 查詢資料:SELECT * FROM 表名 WHERE 條件 GROUP BY 分組 ORDER BY 排序
建立SQLite資料庫的步驟:
a. 建立一個輔助類繼承SQLiteOpenHelper
b. 在輔助類的onCreate()方法中,添加建立表的語句,執行該語句
String str = "CREATE TABLE student (id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),age INTEGER, phone VARCHAR(20))";
db.execSQL(str);
c. 在Activity中,先建立輔助類對象,再用輔助類對象調用getReadableDatabase()或者getWriteableDataBase()建立資料庫物件
備忘:當資料庫和表都已經存在時,onCreate()方法不會再調用
當version發生變化時,會調用onUpgrade()方法
SQL語句執行個體:
(1)增
String str = "INSERT INTO student (name,phone) VALUES (?,?)";
mDB.execSQL(str,new String[]{"小明","1111"});
(2)刪
String str = "DELETE FROM student WHERE name = ?";
mDB.execSQL(str,new String[]{"小明"});
(3)改
String str = "UPDATE student SET name = ?,phone = ? WHERE id = ?";
mDB.execSQL(str,new String[]{"小紅","2222","1"});
(4)查
String str = "SELECT * FROM student";
Cursor cursor = mDB.rawQuery(str,null);
while(cursor.moveToNext()){
//擷取各欄位值的索引
int idIndex = cursor.getColumnIndex("id");
int nameIndex = cursor.getColumnIndex("name");
int phoneIndex = cursor.getColumnIndex("phone");
//根據索引取出資料
String id = cursor.getString(idIndex);
String name = cursor.getString(nameIndex);
String phone = cursor.getString(phoneIndex);
}
ORM 對象關係映射
(1)增
ContentValues values = new ContentsValues();
values.put("name","小白");
values.put("phone","12356");
mDB.insert("student", //表名
null;//規避插入錯誤
values);//插入的值
(2)刪
mDB.delete("student",//表名
"name=?",條件陳述式
new String[]{"小白"});預留位置的填充
(3)改
ContentValues values = new ContentValues();
values.put("phone","45621");
mDB.update("student",//表名
values,//將要修改的值
"name=?",條件陳述式
new String[]{"小白"});//預留位置填充
(4)查
Cursor cursor = mDB.query("student",//表名
null,//查詢的欄位
null,//查詢條件
null,//條件陳述式預留位置的填充
null,//分組語句
null,//分組語句預留位置的填充
null);//排序語句
while(cursor.moveToNext()){
//擷取欄位索引
int nameIndex = cursor.getColumnIndex("name");
//根據索引取出資料
String name = cursor.getString(nameIndex);
}
CursorAdapter遊標適配器
注意:必須含有_id
// 將布局xml檔案轉換成布局對象
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View inflate = getLayoutInflater().inflate(R.layout.list_item, null);
return inflate;
}
// 設定資料顯示
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
TextView textView2 = (TextView) view.findViewById(R.id.textView2);
// 擷取資料
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
// 設定屬性值
textView1.setText(name);
textView2.setText(phone);
}
更新資料
// 重新查詢,得到一個新的cursor對象
Cursor cursor = mDB.query("student", null, null, null, null, null, null);
// 將最新的cursor對象設定到適配器中
mAdapter.changeCursor(cursor);
// 重新整理顯示
mAdapter.notifyDataSetChanged();