Android隨筆(一)

來源:互聯網
上載者:User

標籤:android   資料庫   事務   cursor   索引   

一.SQLite資料庫
  * 1.事務
        beginTransaction();            // 開始事務
        setTransactionSuccessful();    // 設定成功點, 在事務結束時, 成功點之前的操作會被提交
        endTransaction();            // 結束事務, 通常放在finally中, 如果不顯示關閉事務, 逾時也會關閉, 但效率較低
  * 2.另一種增刪改查
        SQLiteDatabase類中有insert(), delete(), update(), query()四個方法, 不用寫SQL語句, 通過參數拼裝得到SQL
        通常使用在ContentProvider

二.ListView
    1.三種Adapter構建ListView
        ListView添加條目的時候, 可以使用setAdapter(ListAdapter)方法, 常用的ListAdapter有三種
            BaseAdapter: 定義一個類繼承BaseAdapter, 重寫4個抽象方法, ListView的條目是由getView()方法構建出來的
            SimpleAdapter: 建立SimpleAdapter對象時, 傳入資料(List<Map<String, ?>>), 並指定資料的綁定關係
            SimpleCursorAdapter: 建立SimpleCursorAdapter對象時, 傳入一個Cursor, 指定資料的綁定關係
    2.監聽ListView的點擊
        調用ListView.setOnItemClickListener(OnItemClickListener)方法註冊一個監聽器
        在監聽器的onItemClick()方法中使用 parent.getItemAtPosition(position) 方法可以擷取指定條目上的資料
            BaseAdapter: 返回的就是自訂的getItem()方法中返回的資料
            SimpleAdapter: 返回的是一個Map, 就是建立SimpleAdapter時List中的一個Map
            SimpleCursorAdapter: 返回的是一個Cursor, 這個Cursor就是建立時傳入的Cursor, 但是已經通過moveToPosition()方法指定到點擊的索引了
            
三.內容提供者(ContentProvider)
    1.什麼是ContentProvider
        ContentProvider可以用來把程式中的資料對外進行共用, 提供增刪改查的方法
        ContentProvider中可以註冊觀察者, 監聽資料的變化
  * 2.怎麼建立
        定義類繼承ContentProvider, 實現抽象方法
        在資訊清單檔中註冊
    3.在手機上註冊
        將應用安裝到手機上即可, 不用運行程式
  * 4.怎麼訪問
        擷取解析器ContentResolver, 指定Uri
        通過ContentResolver.insert(), delete(), update(), query()方法訪問Uri關聯的ContentProvider
    5.Uri的處理
        使用UriMatcher可以檢查傳入的Uri是否和指定的匹配
        如果Uri帶了id, 可以使用ContentUris擷取id, 插入方法可以使用ContentUris給Uri加上id

Android隨筆(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.