Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 記事本範例剖析

編輯:Android 開發實例

Android 內建或官方範例中的記事本程式,是學習 Android 元件整合的經典案例。它涵蓋了 Activity、Intent、ContentProvider、SQLite、CursorAdapter 與 ListView 等常見主題,很適合用來理解一個完整應用程式的運作方式。

程式入口

在 AndroidManifest.xml 中,通常會透過 intent-filter 指定某個 Activity 為啟動入口。當 action 為 MAIN,且 category 為 LAUNCHER 時,系統就會將該 Activity 視為應用程式的主要入口。

NotesList 的作用

記事本程式的入口 Activity 通常會負責顯示筆記清單。它會先設定 Intent 的資料來源,再透過 managedQuery 查詢資料。查詢時會指定需要哪些欄位,例如 ID 與 Title,並設定排序方式。

使用 CursorAdapter 顯示清單

查詢結果會回傳 Cursor,接著建立 SimpleCursorAdapter,將 Cursor 中的標題欄位綁定到 ListView 的項目版面配置。這樣使用者就能看到一筆筆筆記清單。

處理點擊事件

當使用者點選某個項目時,程式會重寫 onListItemClick。它會先取得被點選資料的 URI,再判斷目前的 action。如果是挑選模式,就回傳結果;如果是編輯模式,則啟動 NoteEditor 來編輯該筆資料。

Intent 與 ContentProvider

Android 會根據 Intent 中的 action 與 URI,找到能處理該資料的元件。這裡涉及 ContentProvider 的 authorities、getType 與 UriMatcher。透過這套機制,系統能準確找到 NoteEditor,並開啟對應的編輯畫面。

這個範例清楚展示了 Android 元件之間如何透過 Intent 與 ContentProvider 串接,是理解 Android 資料驅動介面的重要教材。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved