1. 使用 Room 数据库,添加引用库
dependencies {
//https://developer.android.google.cn/jetpack/androidx/releases/room
def room_version = "2.4.3"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}
2. 搭建布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6" />
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="24sp" />
</ScrollView>
<Button
android:id="@+id/bt_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="insert"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/bt_update"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2"
app:layout_constraintVertical_bias="0.2" />
<Button
android:id="@+id/bt_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="update"
app:layout_constraintBottom_toBottomOf="@+id/bt_insert"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/bt_insert"
app:layout_constraintTop_toTopOf="@+id/bt_insert" />
<Button
android:id="@+id/bt_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/bt_delete"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bt_insert" />
<Button
android:id="@+id/bt_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete"
app:layout_constraintBottom_toBottomOf="@+id/bt_clear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/bt_clear"
app:layout_constraintTop_toTopOf="@+id/bt_clear" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. 创建实体类 Word.java
@Entity
public class Word {
@PrimaryKey(autoGenerate = true)
private int id; //主键
@ColumnInfo(name = "english_word")
private String word;
@ColumnInfo(name = "chinese_meaning")
private String chineseMeaning;
public Word() {
}
public Word(String word, String chineseMeaning) {
this.word = word;
this.chineseMeaning = chineseMeaning;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getChineseMeaning() {
return chineseMeaning;
}
public void setChineseMeaning(String chineseMeaning) {
this.chineseMeaning = chineseMeaning;
}
}
4. 创建调用接口
@Dao //Database access object
public interface WordDao {
@Insert
void insertWords(Word... words);
@Update
void updateWords(Word... words);
@Delete
void deleteWords(Word... words);
@Query("DELETE FROM WORD")
void deleteAllWords();
@Query("SELECT * FROM WORD ORDER BY ID DESC")
List<Word> getAllWords();
}
5. 实现抽象类
@Database(entities = {Word.class}, version = 1, exportSchema = false)
public abstract class WordDatabase extends RoomDatabase {
public abstract WordDao getWordDao();
}
6. 调用测试 MainActivity
public class MainActivity extends AppCompatActivity {
WordDatabase wordDatabase;
WordDao wordDao;
TextView textView;
Button btInsert, btUpdate, btDelete, btClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
wordDatabase = Room.databaseBuilder(this, WordDatabase.class, "word_database")
.allowMainThreadQueries()
.build();
wordDao = wordDatabase.getWordDao();
updateView();
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_insert:
Word word1 = new Word("Hello", "你好!");
Word word2 = new Word("World", "世界!");
wordDao.insertWords(word1, word2);
break;
case R.id.bt_clear:
wordDao.deleteAllWords();
break;
case R.id.bt_update:
Word word = new Word("Hi", "嗨");
word.setId(3);
wordDao.updateWords(word);
break;
case R.id.bt_delete:
Word word_d = new Word();
word_d.setId(12);
wordDao.deleteWords(word_d);
break;
}
updateView();
}
};
btInsert = findViewById(R.id.bt_insert);
btClear = findViewById(R.id.bt_clear);
btUpdate = findViewById(R.id.bt_update);
btDelete = findViewById(R.id.bt_delete);
btDelete.setOnClickListener(listener);
btInsert.setOnClickListener(listener);
btClear.setOnClickListener(listener);
btUpdate.setOnClickListener(listener);
}
void updateView() {
List<Word> list = wordDao.getAllWords();
String text = "";
for (int i = 0; i < list.size(); i++) {
Word word = list.get(i);
text += word.getId() + ":" + word.getWord() + "=" + word.getChineseMeaning() + "\n";
}
textView.setText(text);
}
}
7. 存储数据文件及路径,Device File Explorer -> data -> data -> com.xx.xx(项目包名) -> databases, 三个文件 word_database, word_database-shm, word_database-wal .
8. 效果图