編輯:關於Android編程
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyDBHelper extends SQLiteOpenHelper { private static final String COLUMN_ID = "_id"; public static final String TABLE_NAME = "category"; private static final String DATABASE_NAME = "category.db"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + " fid TEXT, token TEXT, cid TEXT, cname TEXT)"; public CategoryDBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME); onCreate(db); } }
public class CategoryDBManager { private MyDBHelper myDBHelper; private static CategoryDBManager categoryDBManager = null; private CategoryDBManager(Context context) { myDBHelper = new MyDBHelper(context); } /** * 單例模式 */ public static CategoryDBManager getInstance(Context context) { if (categoryDBManager == null) { synchronized (CategoryDBManager.class) { if (categoryDBManager == null) { categoryDBManager = new CategoryDBManager(context); } } } return categoryDBManager; } public SQLiteDatabase getDB() { SQLiteDatabase db = myDBHelper.getWritableDatabase(); while (db.isDbLockedByCurrentThread()) { } return db; } public void insertLists(String token, String fid, Listlists) { // 打開可寫數據庫 SQLiteDatabase db = getDB(); for (PlateCategoryData pd : lists) { // 執行SQL語句,替換占位符 db.execSQL("insert into " + MyDBHelper.TABLE_NAME + "(cid, cname, fid, token) values(?, ?, ?, ?)", new Object[] { pd.getId(), pd.getName(), fid, token }); } // 釋放資源 db.close(); } public ArrayList getLists(String fid, String token) { ArrayList datas = new ArrayList (); SQLiteDatabase db = getDB(); // 執行原始查詢,得到cursor String querySql = "select cid, cname from " + MyDBHelper.TABLE_NAME + " where fid = ? and token = ?"; Cursor cursor = db.rawQuery(querySql, new String[] { fid, token }); // 移動cursor到第一個數據(無數據返回false) if (cursor.moveToFirst()) { // while判斷是否有下一條數據 do { PlateCategoryData pd = new PlateCategoryData(cursor.getString(cursor.getColumnIndex("cid")), cursor.getString(cursor.getColumnIndex("cname"))); datas.add(pd); } while (cursor.moveToNext()); } cursor.close(); db.close(); return datas; } public void updateLists(String fid, String token, ArrayList datas) { SQLiteDatabase db = getDB(); for (PlateCategoryData pd : datas) { String sql = "update " + MyDBHelper.TABLE_NAME + " set cid = ?, cname = ? where fid = ? and token = ?"; db.execSQL(sql, new Object[] { pd.getId(), pd.getName(), fid, token }); } db.close(); } public void deleteLists(String fid, String token) { SQLiteDatabase db = getDB(); db.execSQL("delete from " + MyDBHelper.TABLE_NAME + " where fid = ? and token = ?", new Object[] { fid, token }); db.close(); } public void closeDB() { SQLiteDatabase db = getDB(); if (db.isOpen()) { myDBHelper.close(); db.close(); } } }
在完成了 上一篇課程後,你已經有了一個應用。這個應用展示了一個包含一個文本框和一個按鈕的activity(一個單獨的界面)。在這次的課程中,你將會通過在MainActi
什麼是Drawable首先Drawable是一個抽象類,表示的是可以在Canvas中繪制的圖像,常被用作一個view的背景,有多種實現類完成不同的功能。其次Drawabl
最近在項目中寫了一個自定義的倒計時控件,效果是倒計時開始後,紅心逐漸被填充滿。效果如下圖: 分為兩部分:計時器和繪制Bitmap。計時器使用Timer和Time
為了避免看視頻時影響到他人休息,很多用戶會考慮購買無線耳機遠程接收PC的音頻信號。問題是,有多少人會為這種小概率事件去購買無線耳機?如果你身邊有台Andro