Android JetPack架构——结合记事本Demo一篇打通对Sqlite的增删改查结合常用jetpack架构应用

本文介绍了如何在Android中使用JetPack架构组件,特别是ViewModel和LiveData,配合SQLite进行数据操作。通过实例展示了如何创建ViewModel、LiveData、Fragment,并实现了Note的增删改查功能。此外,还探讨了使用RecyclerView和Fragment的技巧,以及如何使用Navigation进行界面管理。
摘要由CSDN通过智能技术生成

static class DeleteAllAsyncTask extends AsyncTask<Note, Void, Void>{

private NoteDao noteDao;

DeleteAllAsyncTask(NoteDao noteDao) {

this.noteDao = noteDao;

}

@Override

protected Void doInBackground(Note… notes) {

noteDao.deleteAllNotes();

return null;

}

}

static class DeleteAsyncTask extends AsyncTask<Note, Void, Void>{

private NoteDao noteDao;

DeleteAsyncTask(NoteDao noteDao) {

this.noteDao = noteDao;

}

@Override

protected Void doInBackground(Note… notes) {

noteDao.deleteNotes(notes);

return null;

}

}

}

[](()3.编写ViewModel+LiveData


[](()为什么要使用ViewModel?

ViewModel 是数据与 UI 分离的中间层,提供了一个将数据转换为 UI 友好型数据的场所。其次,它也提供了多 Fragment 复用相同 ViewModel 的机制。

[](()为什么要使用LiveData?

LiveData 是一个可以感知 Activity 、Fragment生命周期的数据容器。当 LiveData 所持有的数据改变时,它会通知相应的界面代码进行更新。

此处为了方便,数据和界面的交互放在了Activity中,读者有需要可以使用Databinding对Activity进行进一步解耦.

public class NoteViewModel extends AndroidViewModel {

/**

  • 使用数据仓库处理好的数据库交互逻辑

*/

private NoteRepository repository;

public NoteViewModel(@NonNull Application application) {

super(application);

repository = new NoteRepository(application);

}

public LiveData<List> getAllNoteLive() {

return repository.getAllWordLive();

}

public LiveData<List> queryNotesWithPattern(String pattern){

return repository.queryNotesWithPattern(pattern);

}

public void insertNotes(Note… notes){

repository.insertNotes(notes);

}

public void updateNotes(Note… notes){

repository.updateNotes(notes);

}

public void deleteNotes(Note… notes){

repository.deleteNotes(notes);

}

public void deleteAllNotes(){

repository.deleteAllNotes();

}

}

[](()4.编写界面


界面引入了RecyclerView,代替了传统ListView。如果没有使用过RecyclerView可以参照

[Android 控件 RecyclerView](()

其中涉及到矢量图的使用,如果没有使用过矢量图可以参照

[Android中使用矢量图(快速运用)](()

[](()4.1 编写主页的Fragment

fragment_notes.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:id=“@+id/mainFragment”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“.base.NotesFragment”>

<androidx.recyclerview.widget.RecyclerView

android:id=“@+id/recyclerView”

android:layout_width=“match_parent”

android:layout_height=“match_parent” />

<com.google.android.material.floatingactionbutton.FloatingActionButton

android:id=“@+id/floatingActionButton”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“bottom|center”

android:layout_margin=“16dp”

android:clickable=“true”

app:srcCompat=“@drawable/ic_add_white_24dp” />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

cell_card.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:clickable=“true”

android:orientation=“vertical”>

<androidx.cardview.widget.CardView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_marginLeft=“8dp”

android:layout_marginTop=“8dp”

android:layout_marginRight=“8dp”

android:foreground=“?selectableItemBackground”>

<androidx.constraintlayout.widget.ConstraintLayout

android:layout_width=“match_parent”

android:layout_height=“match_

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值