iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 24
0
Mobile Development

iOS Developer Learning Android系列 第 24

iOS Developer Learning Android. Lesson 24 - Room (不好的老師帶你住套房)

  • 分享至 

  • twitterImage
  •  

Room
是個Google官方提供的套件
on base在androidx之上(感覺androidx就是很潮就對了)
用來存取SQLite達到本地資料庫的CRUD(我把它定位成跟CoreDate一樣的東西☘️☘️☘️)

本日效果

說明

  1. 先裝起來
dependencies {
    def room_version = "2.2.0-rc01"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

第二行也要加不然會閃退:"_Impl does not exist"

  1. 分成三個部分
    1. Entity
      就是我們平常在寫的Model,就是資料本身,他們叫做Java bean的指的也是這個
      ORM,Object Relational Mapping,就是在這邊完成
    2. DAO
      Data Access Object,就是幫我們做新刪改查的小夥伴
      其實還是要懂一些SQL語法
    3. Database
      就是存資料的地方,SQLite要操作的對象
  2. 上code
    1. Entity
@Entity(tableName = "note_t")
public class Note
{
    @PrimaryKey(autoGenerate = true)
    public int uid;
    //欄位不同用@ColumnInfo(name = “column_name”)
    public String user;
    public String time;
    public String todo;

    public Note(String user, String time, String todo)
    {
        this.user = user;
        this.time = time;
        this.todo = todo;
    }
}
    2. DAO
@Dao
public interface NoteDAO
{
    @Insert
    public void insert(Note note);

    @Query("SELECT * FROM note_t WHERE user = :user")
    List<Note> getAllNotesBy(String user);
}
    3. Database
@Database(entities = {Note.class}, version = 1)//版本是要幹嘛的?
public abstract class IDLADB extends RoomDatabase
{
    public abstract NoteDAO noteDAO();
    //Singleton
    private static IDLADB INSTANCE;
    public static IDLADB getDatabase(Context context)
    {
        if (INSTANCE == null)
        {
            INSTANCE = Room.databaseBuilder(context,IDLADB.class,"idlaDB")
                    .allowMainThreadQueries()//其實這種耗時的動作應該丟到背景去執行,特殊原因要強制就家這句
                    .build();
        }
        return INSTANCE;
    }
}
  1. 透過上述的設定將三者串連起來,我們就可以在程式使用了
    1. IDLADB.getDatabase(this).noteDAO();
      拿到DAO
    2. noteDAO.insert(newNote);
      透過DAO輸入
    3. noteDAO.getAllNotesBy("mark");
      透過DAO輸出
  2. 這次用了SimpleAdapter跟simple_list_item_2處理畫面,比較少見
        simpleAdapter = new SimpleAdapter(this,
                maps,
                android.R.layout.simple_list_item_2,
                new String[] {"time", "todo"},
                new int[] {android.R.id.text1,android.R.id.text2});
        
        for(Note note: notes)
        {
            HashMap<String,String> map = new HashMap<String,String>();
            map.put( "time", note.time);
            map.put( "todo", note.todo);
            maps.add(map);
        }

        listView.setAdapter(simpleAdapter);
  1. 導出DB
    有的時候我們會想要備份,或是用browser去分析資料
    這時候就需要把DB export出來
    試了這個(導出來的DB是空的)跟那個(導出來的DB竟然要密碼?!?!)
    最後才發現AS就有Device File Explorer
    Shift兩次叫出來之後就可以照下面的路徑找到


    然後終於可以看到數據啦~

參考資料

今天的範例程式

可以去 https://github.com/mark33699/IDLA 看一下順便給顆⭐️


如果你喜歡我的影片別忘了按讚分享加訂閱,開啟紅色的小鈴鐺,我們明天見~


上一篇
iOS Developer Learning Android. Lesson 23 - 本地儲存 (可以不要用SQL嗎?)
下一篇
iOS Developer Learning Android. Lesson 25 - ActionBar (就算沒有NavigationController還是要處理那條bar喔)
系列文
iOS Developer Learning Android30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言