android學習筆記7

來源:互聯網
上載者:User

 在使用adb shell中使用sqlite3工具顯示中文時,在console上顯示的中文字元為亂碼,原因在於cmd中的字元集,解決方案,
    在進入cmd中,輸入 chcp 65001
   
    ① 完全通過SQLiteDatabase   ② SQLiteOpenHelper  ③ 使用ContentProvider
    ④ 資料庫檔案的管理
   
    ①:
      SQLiteDatabase sqlite = openOrCreateDatabase("diary.db", MODE, null);
     
    使用Cursor進行ListView資料繫結:
            例如:
      SQLiteDatabase sqlite = openOrCreateDatabase("diary.db", MODE, null);
     Cursor cur = sqlite.query("diary", new String[] {"_id", "title", "content"},
                   null, null, null, null, null);
     // 使用Cursor為Listview綁定資料
     SimpleCursorAdapter curAdapter = new SimpleCursorAdapter(
         this, android.R.layout.simple_list_item_2,
         cur, new String[] {"title", "content"},
         new int[] {android.R.id.text1, android.R.id.text2}
     );
     diaryList.setAdapter(curAdapter);
     
     指定過程與SimpleAdapter類似;
     
     CursorAdapter的使用:
          sqlite = openOrCreateDatabase("diary.db",
       MODE, null);
     cur = sqlite.query("diary", new String[] {"_id", "title", "content"},
       null, null, null, null, null);
       
      CursorAdapter cAdapter = new CursorAdapter(this, cur) {

   @Override
   public void bindView(View view, Context context, Cursor cursor) {
    
    ((TextView)view).setText(cursor.getString(cursor.getColumnIndex("title")));
   }

   @Override
   public View newView(Context context, Cursor cursor, ViewGroup parent) {
    TextView titleLabel = new TextView(MainActivity.this);
    
    return titleLabel;
   }
      
     };
     SimpleCursorAdapter,CursorAdapter需要綁定遊標

      SQLiteOpenHelper中處理的資料庫,開關,更新
      SQLiteDatabase用以具體的資料庫操作
     
      要獲得SQLiteOpenHelper的對象,需要寫一個類extends SQLiteOpenHelper類,重寫構造,onCreate,onUpgrade方法;
     
      資料庫檔案操作:直接指定資料庫檔案所在位置/sdcard/databases/diary_db
      在許可權控制中,在另一個程式中的擷取資料庫檔案需要使用完整路徑;
     
      檔案許可權:對外許可權公開[rwx],則資料庫檔案公開;
                若檔案許可權不公開,但類與其同一包路徑,訪問頁不受限制

ContentProvider:
      寫一個類繼承ContentProvider類,需要重寫實際進行資料庫操作的方法,在Manifest檔案中配置 <provider />組件;
      配置的兩個屬性android:name的值為繼承ContentProvider類的類名,android:authorities值可以隨意指定,目前其子類放於
      與主目錄在同一包下,在使用ContentResolver進行CRUD操作時,進行Uri對象的建立,content://<android:authorities值>/表名/[#]
      其中#號代表的記錄,可以是具體的數字,標識記錄id號;
     
 
      優點:① 業務與資料庫分離  ② 共用資料,主要通過授權機制完成
     
     
    在Activity中使用Uri對象解析uri地址,對應到Manifest中的註冊的provider標籤的ContentProvider組件,找到ContentProvider子類,
在Activity中擷取getContentResolver()方法,擷取ContentResolver對象,常使用的ContentProvider方法有intert(),delete(),query(),
update(),在調用ContentProvider相應方法時,實際使用的是ContentProvider子類中的相應方法,在onCreate方法用以建立SQLiteDatabase
對象。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.