SearchView一个类似百度搜索框关键字搜索效果的实现
https://blog.csdn.net/qq_42177292/article/details/81158423

Room数据库详实使用指北(附Demo)
https://blog.csdn.net/langlitaojin/article/details/88400659
https://blog.csdn.net/chenrenxiang/article/details/84067783
https://zhuanlan.zhihu.com/p/77036077

1.在app的build.gradle中加入room的相关依赖。

def room_version = "2.2.0-alpha01"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

2.创建一个关于学生的Entity,即创建一张学生表。

@Entity(tableName = "student")
public class Student
{
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id", typeAffinity = ColumnInfo.INTEGER)
    public int id;

    @ColumnInfo(name = "name", typeAffinity = ColumnInfo.TEXT)
    public String name;

    @ColumnInfo(name = "age", typeAffinity = ColumnInfo.TEXT)
    public String age;

    /**
     * Room会使用这个构造器来存储数据,也就是当你从表中得到Student对象时候,Room会使用这个构造器
     * */
    public Student(int id, String name, String age)
    {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    /**
     * 由于Room只能识别和使用一个构造器,如果希望定义多个构造器,你可以使用Ignore标签,让Room忽略这个构造器
     * 同样,@Ignore标签还可用于字段,使用@Ignore标签标记过的字段,Room不会持久化该字段的数据
     * */
    @Ignore
    public Student(String name, String age)
    {
        this.name = name;
        this.age = age;
    }
}

3.针对以上学生Entity,我们需要定义一个Dao接口文件,以完成对Entity的访问。注意:在文件的上方,需要加入@Dao标签。

@Dao
public interface StudentDao
{
    @Insert
    void insertStudent(Student student);

    @Delete
    void deleteStudent(Student student);

    @Update
    void updateStudent(Student student);

    @Query("SELECT * FROM student")
    List<Student> getStudentList();

    @Query("SELECT * FROM student WHERE id = :id")
    Student getStudentById(int id);
}

4.定义好Entity和Dao后,接下去就是创建数据库了。

@Database(entities = {Student.class}, version = 1)
public abstract class MyDatabase extends RoomDatabase
{
    private static final String DATABASE_NAME = "my_db";

    private static MyDatabase databaseInstance;

    public static synchronized MyDatabase getInstance(Context context)
    {
        if(databaseInstance == null)
        {
            databaseInstance = Room
                    .databaseBuilder(context.getApplicationContext(), MyDatabase.class, DATABASE_NAME)
                    .build();
        }
        return databaseInstance;
    }

    public abstract StudentDao studentDao();
}

由于我们采用单例模式来实例化数据库,所以我们可以这样得到数据库对象:

MyDatabase myDatabase = MyDatabase.getInstance(this);
插入数据:
myDatabase.studentDao().insertStudent(new Student(name, age));

更新数据:
myDatabase.studentDao().updateStudent(new Student(id, name, age));

删除数据:
myDatabase.studentDao().deleteStudent(student);

查询所有学生:
myDatabase.studentDao().getStudentList();

查询某个学生:
myDatabase.studentDao().getStudentById(id);

这些对数据库的操作方法都是我们之前在Dao文件中已经定义好的。需要注意的是,不能直接在UI线程中执行这些操作,需要放在工作线程中进行。例如,我们可以使用AsyncTask来进行查询操作。

private class QueryStudentTask extends AsyncTask<Void, Void, Void>
{
    public QueryStudentTask()
    {

    }

    @Override
    protected Void doInBackground(Void... arg0)
    {
        studentList.clear();
        studentList.addAll(myDatabase.studentDao().getStudentList());
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        studentAdapter.notifyDataSetChanged();
    }
}
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐