说在前面:

1、使用Room需要添加的依赖:

dependencies {
def room_version = "2.2.3" implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor // optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version" // optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version" // optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version" // Test helpers
testImplementation "androidx.room:room-testing:$room_version"
}

2、数据库可视化工具安装及使用说明

http://www.sqlitebrowser.org/

3、涉及到的知识:

工程案例:

一、APP描述:对Word实体进行增删改

二、编写思路:

1、画界面:

1)上边是一个ScrollView(数据多的时候可滑动),ScrollView内有一个TextView

2)下边是四个按键,分别代表,插入、删除、删除所有、修改。

2、创建实体(Entity):

package com.me.roombasic;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey; @Entity
public class Word {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "姓名")
private String name;
@ColumnInfo(name = "外号")
private String other; public Word(String name, String other) {
this.name = name;
this.other = other;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getOther() {
return other;
} public void setOther(String other) {
this.other = other;
}
}

3、创建dao

package com.me.roombasic;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update; import java.util.List; @Dao
public interface WordDao {
@Insert
void insertWord(Word ...words ); @Update
void updateWord(Word... words); @Delete
void deleteWord(Word... words); @Query("DELETE FROM WORD")
void deleteAllWords(); @Query("SELECT * FROM WORD ORDER BY ID DESC")
List<Word> getAllWords(); }

4、创建database

package com.me.roombasic;

import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities = {Word.class},version = 1,exportSchema = false)
public abstract class WordDatabase extends RoomDatabase {
public abstract WordDao getWordDao(); }

5、暂时在mainActive.java中为TextView、Button绑定数据和监听:

package com.me.roombasic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room; import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity {
WordDao wordDao;
WordDatabase wordDatabase;
Button buttonInsert,buttonUpdate,buttonDelete,buttonQuery;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
buttonInsert = findViewById(R.id.buttoninster);
buttonUpdate = findViewById(R.id.buttonupdate);
buttonDelete = findViewById(R.id.buttondelete);
buttonQuery = findViewById(R.id.buttonquery);
wordDatabase = Room.databaseBuilder(this,WordDatabase.class,"word_database")
.allowMainThreadQueries()
.build();
wordDao = wordDatabase.getWordDao();
updateView();
buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Word word1 = new Word("谷子腾","恶霸");
Word word2 = new Word("张凯鑫","胖鑫");
Word word3 = new Word("王正帅","笑天");
wordDao.insertWord(word1,word2,word3);
updateView();
}
});
buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Word word2 = new Word("张凯鑫","bujv");
word2.setId(2);
wordDao.updateWord(word2);
updateView();
}
});
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Word word2 = new Word("张凯鑫","胖鑫");
word2.setId(3);
wordDao.deleteWord(word2);
updateView();
}
});buttonQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wordDao.deleteAllWords();
updateView();
}
});
} void updateView(){
List<Word> list = wordDao.getAllWords();
String text = "";
for(int i=0;i<list.size();i++){
text += list.get(i).getId() + ":" + list.get(i).getName() + "=" + list.get(i).getOther() + "\n";
}
textView.setText(text);
}
}

6、效果演示:

未完待续。。。。

最新文章

  1. css学习笔记 9
  2. SAMBA 共享服务器搭建
  3. 获取 input 单选框和多选框的值
  4. JS实现移动端图片延迟加载
  5. mssql全文索引
  6. PostgreSQL Errors and Messages
  7. 某代理网站免费IP地址抓取测试
  8. Java 动态编译组件 &amp; 类动态加载
  9. Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)
  10. javascript笔记05:函数表达式和函数语句的区别
  11. devi into python 笔记(五)异常 文件操作 sys os glob模块简单实用
  12. 【HDOJ】3587 NUDOTA
  13. Unity 之 Redux 模式(第一篇)—— 人物移动
  14. shell 多进程
  15. jar包后台启动--nohup篇
  16. Linux主机之间ssh免密登录配置方法
  17. angularjs $$phase
  18. Rsync 服务器端配置
  19. 本机Tomcat启动myeclipse,用Jmeter录制脚本端口冲突解决办法
  20. [转]OBJECT_ID 有哪些种类

热门文章

  1. ASM ClassReader failed to parse class file
  2. redis学习笔记-03:redis安装
  3. spyder崩溃修复
  4. 「牛客CSP-S2019赛前集训营2」服务器需求
  5. 105、Java中String类之利用indexOf()方法判断子字符串是否存在
  6. 产品原型 UI 设计工具
  7. redis缓存穿透,缓存击穿,缓存雪崩问题
  8. LR、SVM、RF、GBDT、XGBoost和LightGbm比较
  9. CCF 201703-4 地铁修建(最小生成树)
  10. 解决Google浏览器不能打开kubernetes dashboard方法【转】