Android Studio 中使用 SQLite 数据库开发完整指南(Kotlin版本)

news2025/5/10 6:09:11

在这里插入图片描述

文章目录

    • 1. 项目准备
      • 1.1 创建新项目
      • 1.2 添加必要依赖
    • 2. 数据库设计
    • 3. 实现数据库
      • 3.1 创建实体类 (Entity)
      • 3.2 创建数据访问对象 (DAO)
      • 3.3 创建数据库类
    • 4. 创建 Repository
    • 5. 创建 ViewModel
    • 6. 实现 UI 层
      • 6.1 创建笔记列表 Activity
        • activity_notes_list.xml
        • NotesListActivity.kt
      • 6.2 创建笔记详情 Activity
        • activity_note_detail.xml
        • NoteDetailActivity.kt
      • 6.3 创建 RecyclerView Adapter
      • 6.4 创建 Application 类
    • 7. 添加菜单资源
    • 8. 添加字符串资源
    • 9. 添加图标资源
    • 10. 运行和测试应用
    • 11. 数据库调试技巧
      • 11.1 查看数据库内容
      • 11.2 使用 Stetho 进行调试
    • 12. 数据库迁移
      • 12.1 修改实体类
      • 12.2 更新数据库版本
      • 12.3 添加迁移策略
    • 13. 性能优化建议
    • 14. 完整项目结构
    • 15. 总结

在这里插入图片描述

1. 项目准备

1.1 创建新项目

  1. 打开 Android Studio
  2. 选择 “Start a new Android Studio project”
  3. 选择 “Empty Activity” 模板
  4. 设置项目名称(例如 “SQLiteDemo”)
  5. 选择语言(Kotlin 或 Java,本教程以 Kotlin 为例)
  6. 设置最低 API 级别(建议 API 21 或更高)
  7. 点击 “Finish” 完成项目创建

1.2 添加必要依赖

确保 build.gradle (Module: app) 中包含以下依赖:

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    
    // Room 数据库(SQLite 的抽象层)
    implementation "androidx.room:room-runtime:2.4.2"
    implementation "androidx.room:room-ktx:2.4.2"
    kapt "androidx.room:room-compiler:2.4.2"
    
    // 协程支持
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
    
    // ViewModel 和 LiveData
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
    
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

点击 “Sync Now” 同步项目。

2. 数据库设计

假设我们要创建一个简单的笔记应用,包含以下数据表:

  • notes 表:
    • id: 主键,自增
    • title: 笔记标题
    • content: 笔记内容
    • created_at: 创建时间
    • updated_at: 更新时间

3. 实现数据库

3.1 创建实体类 (Entity)

com.yourpackage.model 包下创建 Note.kt 文件:

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.*

@Entity(tableName = "notes")
data class Note(
    @PrimaryKey(autoGenerate = true)
    val id: Long = 0,
    
    var title: String,
    var content: String,
    
    val created_at: Date = Date(),
    var updated_at: Date = Date()
)

3.2 创建数据访问对象 (DAO)

com.yourpackage.dao 包下创建 NoteDao.kt 文件:

import androidx.lifecycle.LiveData
import androidx.room.*
import com.yourpackage.model.Note

@Dao
interface NoteDao {
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertNote(note: Note): Long
    
    @Update
    suspend fun updateNote(note: Note)
    
    @Delete
    suspend fun deleteNote(note: Note)
    
    @Query("SELECT * FROM notes ORDER BY updated_at DESC")
    fun getAllNotes(): LiveData<List<Note>>
    
    @Query("SELECT * FROM notes WHERE id = :noteId")
    suspend fun getNoteById(noteId: Long): Note?
    
    @Query("SELECT * FROM notes WHERE title LIKE :query OR content LIKE :query ORDER BY updated_at DESC")
    fun searchNotes(query: String): LiveData<List<Note>>
}

3.3 创建数据库类

com.yourpackage.database 包下创建 AppDatabase.kt 文件:

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.yourpackage.dao.NoteDao
import com.yourpackage.model.Note

@Database(entities = [Note::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    
    abstract fun noteDao(): NoteDao
    
    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null
        
        fun getDatabase(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "notes_database"
                )
                    .fallbackToDestructiveMigration() // 数据库升级策略,简单应用可以这样设置
                    .build()
                INSTANCE = instance
                instance
            }
        }
    }
}

4. 创建 Repository

com.yourpackage.repository 包下创建 NoteRepository.kt 文件:

import androidx.lifecycle.LiveData
import com.yourpackage.dao.NoteDao
import com.yourpackage.model.Note
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class NoteRepository(private val noteDao: NoteDao) {
    
    val allNotes: LiveData<List<Note>> = noteDao.getAllNotes()
    
    suspend fun insert(note: Note): Long {
        return withContext(Dispatchers.IO) {
            noteDao.insertNote(note)
        }
    }
    
    suspend fun update(note: Note) {
        withContext(Dispatchers.IO) {
            note.updated_at = Date()
            noteDao.updateNote(note)
        }
    }
    
    suspend fun delete(note: Note) {
        withContext(Dispatchers.IO) {
            noteDao.deleteNote(note)
        }
    }
    
    suspend fun getNoteById(id: Long): Note? {
        return withContext(Dispatchers.IO) {
            noteDao.getNoteById(id)
        }
    }
    
    fun searchNotes(query: String): LiveData<List<Note>> {
        return noteDao.searchNotes("%$query%")
    }
}

5. 创建 ViewModel

com.yourpackage.viewmodel 包下创建 NoteViewModel.kt 文件:

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.yourpackage.model.Note
import com.yourpackage.repository.NoteRepository
import kotlinx.coroutines.launch

class NoteViewModel(private val repository: NoteRepository) : ViewModel() {
    
    val allNotes = repository.allNotes
    
    fun insert(note: Note) = viewModelScope.launch {
        repository.insert(note)
    }
    
    fun update(note: Note) = viewModelScope.launch {
        repository.update(note)
    }
    
    fun delete(note: Note) = viewModelScope.launch {
        repository.delete(note)
    }
    
    fun getNoteById(id: Long) = viewModelScope.launch {
        repository.getNoteById(id)
    }
    
    fun searchNotes(query: String) = repository.searchNotes(query).asLiveData()
}

class NoteViewModelFactory(private val repository: NoteRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(NoteViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return NoteViewModel(repository) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

6. 实现 UI 层

6.1 创建笔记列表 Activity

创建 NotesListActivity.kt 和对应的布局文件 activity_notes_list.xml

activity_notes_list.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.NotesListActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.SQLiteDemo.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.SQLiteDemo.PopupOverlay"
            app:title="@string/app_name" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/search_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/search_input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/search_hint"
                android:imeOptions="actionSearch"
                android:inputType="text" />
        </com.google.android.material.textfield.TextInputLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/notes_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="72dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_add_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:contentDescription="@string/add_note"
        android:src="@drawable/ic_add"
        app:backgroundTint="@color/purple_500"
        app:tint="@android:color/white" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
NotesListActivity.kt
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.yourpackage.R
import com.yourpackage.adapter.NotesAdapter
import com.yourpackage.databinding.ActivityNotesListBinding
import com.yourpackage.model.Note
import com.yourpackage.viewmodel.NoteViewModel
import com.yourpackage.viewmodel.NoteViewModelFactory

class NotesListActivity : AppCompatActivity() {
    
    private lateinit var binding: ActivityNotesListBinding
    private lateinit var notesAdapter: NotesAdapter
    
    private val viewModel: NoteViewModel by viewModels {
        NoteViewModelFactory((application as NotesApplication).repository)
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityNotesListBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
        setSupportActionBar(binding.toolbar)
        
        setupRecyclerView()
        setupSearch()
        setupFAB()
        observeNotes()
    }
    
    private fun setupRecyclerView() {
        notesAdapter = NotesAdapter { note ->
            // 点击笔记项时的操作
            val intent = Intent(this, NoteDetailActivity::class.java).apply {
                putExtra(NoteDetailActivity.EXTRA_NOTE_ID, note.id)
            }
            startActivity(intent)
        }
        
        binding.notesRecyclerView.apply {
            layoutManager = LinearLayoutManager(this@NotesListActivity)
            adapter = notesAdapter
            setHasFixedSize(true)
        }
    }
    
    private fun setupSearch() {
        binding.searchInput.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = binding.searchInput.text.toString().trim()
                if (query.isNotEmpty()) {
                    viewModel.searchNotes(query).observe(this) { notes ->
                        notesAdapter.submitList(notes)
                    }
                } else {
                    observeNotes() // 如果查询为空,返回所有笔记
                }
                true
            } else {
                false
            }
        }
    }
    
    private fun setupFAB() {
        binding.fabAddNote.setOnClickListener {
            val intent = Intent(this, NoteDetailActivity::class.java)
            startActivity(intent)
        }
    }
    
    private fun observeNotes() {
        viewModel.allNotes.observe(this) { notes ->
            notesAdapter.submitList(notes)
        }
    }
    
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }
    
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_delete_all -> {
                deleteAllNotes()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
    
    private fun deleteAllNotes() {
        viewModel.allNotes.value?.let { notes ->
            if (notes.isNotEmpty()) {
                for (note in notes) {
                    viewModel.delete(note)
                }
                Snackbar.make(binding.root, "All notes deleted", Snackbar.LENGTH_SHORT).show()
            }
        }
    }
}

6.2 创建笔记详情 Activity

创建 NoteDetailActivity.kt 和对应的布局文件 activity_note_detail.xml

activity_note_detail.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.NoteDetailActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.SQLiteDemo.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.SQLiteDemo.PopupOverlay" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/title_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/title_input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/title_hint"
                    android:inputType="textCapSentences|textAutoCorrect"
                    android:maxLines="1" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/content_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/content_input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/content_hint"
                    android:inputType="textMultiLine|textCapSentences|textAutoCorrect"
                    android:minLines="5"
                    android:gravity="top" />
            </com.google.android.material.textfield.TextInputLayout>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:contentDescription="@string/save_note"
        android:src="@drawable/ic_save"
        app:backgroundTint="@color/purple_500"
        app:tint="@android:color/white" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
NoteDetailActivity.kt
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import com.yourpackage.R
import com.yourpackage.databinding.ActivityNoteDetailBinding
import com.yourpackage.model.Note
import com.yourpackage.viewmodel.NoteViewModel
import com.yourpackage.viewmodel.NoteViewModelFactory
import java.util.*

class NoteDetailActivity : AppCompatActivity() {
    
    companion object {
        const val EXTRA_NOTE_ID = "extra_note_id"
    }
    
    private lateinit var binding: ActivityNoteDetailBinding
    private val viewModel: NoteViewModel by viewModels {
        NoteViewModelFactory((application as NotesApplication).repository)
    }
    
    private var noteId: Long = -1L
    private var isNewNote = true
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityNoteDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
        setSupportActionBar(binding.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        
        noteId = intent.getLongExtra(EXTRA_NOTE_ID, -1L)
        isNewNote = noteId == -1L
        
        if (!isNewNote) {
            loadNote()
        }
        
        setupSaveButton()
        setupTextWatchers()
    }
    
    private fun loadNote() {
        viewModel.getNoteById(noteId)
        viewModel.allNotes.observe(this) { notes ->
            notes.find { it.id == noteId }?.let { note ->
                binding.titleInput.setText(note.title)
                binding.contentInput.setText(note.content)
            }
        }
    }
    
    private fun setupSaveButton() {
        binding.fabSave.setOnClickListener {
            saveNote()
        }
    }
    
    private fun setupTextWatchers() {
        binding.titleInput.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable?) {
                validateInputs()
            }
        })
        
        binding.contentInput.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable?) {
                validateInputs()
            }
        })
    }
    
    private fun validateInputs(): Boolean {
        val titleValid = binding.titleInput.text?.isNotBlank() ?: false
        val contentValid = binding.contentInput.text?.isNotBlank() ?: false
        
        binding.titleLayout.error = if (!titleValid) getString(R.string.title_required) else null
        binding.contentLayout.error = if (!contentValid) getString(R.string.content_required) else null
        
        return titleValid && contentValid
    }
    
    private fun saveNote() {
        if (!validateInputs()) return
        
        val title = binding.titleInput.text.toString()
        val content = binding.contentInput.text.toString()
        
        if (isNewNote) {
            val note = Note(title = title, content = content)
            viewModel.insert(note)
            Snackbar.make(binding.root, "Note saved", Snackbar.LENGTH_SHORT).show()
            finish()
        } else {
            viewModel.allNotes.value?.find { it.id == noteId }?.let { existingNote ->
                val updatedNote = existingNote.copy(
                    title = title,
                    content = content,
                    updated_at = Date()
                )
                viewModel.update(updatedNote)
                Snackbar.make(binding.root, "Note updated", Snackbar.LENGTH_SHORT).show()
                finish()
            }
        }
    }
    
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            android.R.id.home -> {
                onBackPressed()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}

6.3 创建 RecyclerView Adapter

com.yourpackage.adapter 包下创建 NotesAdapter.kt 文件:

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.yourpackage.R
import com.yourpackage.databinding.ItemNoteBinding
import com.yourpackage.model.Note
import java.text.SimpleDateFormat
import java.util.*

class NotesAdapter(private val onItemClick: (Note) -> Unit) :
    ListAdapter<Note, NotesAdapter.NoteViewHolder>(NoteDiffCallback()) {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
        val binding = ItemNoteBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return NoteViewHolder(binding, onItemClick)
    }
    
    override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
    
    class NoteViewHolder(
        private val binding: ItemNoteBinding,
        private val onItemClick: (Note) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        
        fun bind(note: Note) {
            binding.apply {
                noteTitle.text = note.title
                noteContent.text = note.content
                
                val dateFormat = SimpleDateFormat("MMM dd, yyyy - hh:mm a", Locale.getDefault())
                noteDate.text = dateFormat.format(note.updated_at)
                
                root.setOnClickListener {
                    onItemClick(note)
                }
            }
        }
    }
    
    private class NoteDiffCallback : DiffUtil.ItemCallback<Note>() {
        override fun areItemsTheSame(oldItem: Note, newItem: Note): Boolean {
            return oldItem.id == newItem.id
        }
        
        override fun areContentsTheSame(oldItem: Note, newItem: Note): Boolean {
            return oldItem == newItem
        }
    }
}

创建对应的列表项布局文件 item_note.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:id="@+id/note_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/note_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:ellipsize="end"
            android:maxLines="2"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@android:color/darker_gray" />

        <TextView
            android:id="@+id/note_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Caption"
            android:textColor="@android:color/darker_gray" />
    </LinearLayout>
</com.google.android.material.card.MaterialCardView>

6.4 创建 Application 类

com.yourpackage 包下创建 NotesApplication.kt 文件:

import android.app.Application
import com.yourpackage.database.AppDatabase
import com.yourpackage.repository.NoteRepository

class NotesApplication : Application() {
    
    val database by lazy { AppDatabase.getDatabase(this) }
    val repository by lazy { NoteRepository(database.noteDao()) }
}

更新 AndroidManifest.xml 文件,添加 android:name 属性:

<application
    android:name=".NotesApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.SQLiteDemo">
    <!-- 其他配置 -->
</application>

7. 添加菜单资源

res/menu 目录下创建 menu_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <item
        android:id="@+id/action_delete_all"
        android:icon="@drawable/ic_delete"
        android:title="@string/delete_all"
        app:showAsAction="never" />
</menu>

8. 添加字符串资源

res/values/strings.xml 文件中添加以下字符串:

<resources>
    <string name="app_name">SQLite Notes</string>
    <string name="title_hint">Title</string>
    <string name="content_hint">Content</string>
    <string name="search_hint">Search notes...</string>
    <string name="add_note">Add new note</string>
    <string name="save_note">Save note</string>
    <string name="delete_all">Delete all notes</string>
    <string name="title_required">Title is required</string>
    <string name="content_required">Content is required</string>
</resources>

9. 添加图标资源

确保在 res/drawable 目录下有以下矢量图标:

  • ic_add.xml (添加按钮图标)
  • ic_save.xml (保存按钮图标)
  • ic_delete.xml (删除按钮图标)

10. 运行和测试应用

现在,您可以运行应用程序并测试以下功能:

  1. 添加新笔记
  2. 查看笔记列表
  3. 编辑现有笔记
  4. 删除笔记
  5. 搜索笔记
  6. 删除所有笔记

11. 数据库调试技巧

11.1 查看数据库内容

  1. 在 Android Studio 中打开 “Device File Explorer” (View -> Tool Windows -> Device File Explorer)
  2. 导航到 /data/data/com.yourpackage/databases/
  3. 找到 notes_database 文件
  4. 右键点击并选择 “Save As” 将其保存到本地
  5. 使用 SQLite 浏览器工具(如 DB Browser for SQLite)打开该文件查看内容

11.2 使用 Stetho 进行调试

添加 Stetho 依赖到 build.gradle:

implementation 'com.facebook.stetho:stetho:1.6.0'

NotesApplication.kt 中初始化 Stetho:

import com.facebook.stetho.Stetho

class NotesApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        Stetho.initializeWithDefaults(this)
    }
    
    // 其他代码...
}

运行应用后,在 Chrome 浏览器中访问 chrome://inspect 可以查看和调试数据库。

12. 数据库迁移

当您需要更改数据库结构时(例如添加新表或修改现有表),需要进行数据库迁移。

12.1 修改实体类

例如,我们要为 Note 添加一个 is_pinned 字段:

@Entity(tableName = "notes")
data class Note(
    // 现有字段...
    var is_pinned: Boolean = false
)

12.2 更新数据库版本

修改 AppDatabase.kt:

@Database(entities = [Note::class], version = 2, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    // ...
}

12.3 添加迁移策略

val migration1to2 = object : Migration(1, 2) {
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL("ALTER TABLE notes ADD COLUMN is_pinned INTEGER NOT NULL DEFAULT 0")
    }
}

// 在 databaseBuilder 中添加迁移
val instance = Room.databaseBuilder(
    context.applicationContext,
    AppDatabase::class.java,
    "notes_database"
)
    .addMigrations(migration1to2)
    .build()

13. 性能优化建议

  1. 使用事务:对于批量操作,使用事务可以显著提高性能:
@Dao
interface NoteDao {
    @Transaction
    suspend fun insertAll(notes: List<Note>) {
        notes.forEach { insertNote(it) }
    }
}
  1. 索引优化:为常用查询字段添加索引:
@Entity(tableName = "notes", indices = [Index(value = ["title"], unique = false)])
data class Note(
    // ...
)
  1. 分页加载:对于大量数据,使用 Paging 库:
@Query("SELECT * FROM notes ORDER BY updated_at DESC")
fun getPagedNotes(): PagingSource<Int, Note>
  1. 避免在主线程操作数据库:始终确保数据库操作在后台线程执行。

14. 完整项目结构

最终项目结构应类似于:

com.yourpackage
├── adapter
│   └── NotesAdapter.kt
├── dao
│   └── NoteDao.kt
├── database
│   └── AppDatabase.kt
├── model
│   └── Note.kt
├── repository
│   └── NoteRepository.kt
├── ui
│   ├── NotesListActivity.kt
│   └── NoteDetailActivity.kt
├── viewmodel
│   ├── NoteViewModel.kt
│   └── NoteViewModelFactory.kt
└── NotesApplication.kt

15. 总结

本指南详细介绍了在 Android Studio 中使用 SQLite 数据库的完整开发流程,包括:

  1. 设置项目和依赖
  2. 设计数据库结构
  3. 实现 Room 数据库组件(Entity, DAO, Database)
  4. 创建 Repository 层
  5. 实现 ViewModel
  6. 构建用户界面
  7. 添加数据库迁移支持
  8. 性能优化建议

通过遵循这些步骤,您可以构建一个功能完善、结构清晰的 Android 应用,充分利用 SQLite 数据库的强大功能。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2372067.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Spring 框架实战:如何实现高效的依赖注入,优化项目结构?

Spring 框架实战&#xff1a;如何实现高效的依赖注入&#xff0c;优化项目结构&#xff1f; 在当今的 Java 开发领域&#xff0c;Spring 框架占据着举足轻重的地位。而依赖注入作为 Spring 的核心概念之一&#xff0c;对于构建高效、灵活且易于维护的项目结构有着关键作用。本…

C++ learning day 01

目录 1. iostream : 2.第一个C++程序 3. 执行过程以及以上例子详解(以上例子为参考) 1. iostream : 全称: input/output stream library 作用: 用于处理输入输出操作 2.第一个C++程序 #include <iostream>int main() {std::cout << "Hello World! &qu…

李沐《动手学深度学习》 | 多层感知机

文章目录 感知机模型《深度学习入门》的解释训练感知机损失函数的选择感知机的收敛定理&#xff1a;什么时候能够停下来&#xff0c;是不是真的可以停下来感知机的不足 多层感知模型案例引入隐藏层从线性到非线性单隐藏层-单分类案例多隐藏层 激活函数softmax函数溢出的问题 多…

vue教程(vuepress版)

Vue 完全指南 项目介绍 这是一个系统化的 Vue.js 学习教程&#xff0c;采用循序渐进的方式&#xff0c;帮助开发者从零开始掌握 Vue 开发技能。 教程特点 循序渐进: 从 Vue 基础概念开始&#xff0c;逐步深入到高级特性&#xff0c;适合不同层次的开发者学习实战驱动: 结合…

【网络原理】深入理解HTTPS协议

本篇博客给大家带来的是网络原理的知识点,本篇解释了为什么有HTTP还要发展HTTPS协议. &#x1f40e;文章专栏: JavaEE初阶 &#x1f680;若有问题 评论区见 ❤ 欢迎大家点赞 评论 收藏 分享 如果你不知道分享给谁,那就分享给薯条. 你们的支持是我不断创作的动力 . 王子,公主请阅…

Linux上将conda环境VLLM服务注册为开机自启

这里写目录标题 一、Systemd服务方式1、编写启动脚本2、保存脚本并赋予权限3、创建 systemd 服务单元文件3、 启用并测试服务4、停止systemd服务 二、Crontab方式1、编辑crontab文件2、添加开机启动任务 参考链接 项目需要vllm进行模型支撑&#xff0c;所以需要做成开机自启保证…

k8s的pod挂载共享内存

k8s的pod挂载共享内存&#xff0c;限制不生效问题&#xff1a; 注&#xff1a;/dev/shm 是 Linux 系统中用于共享内存的特殊路径。通过将 emptyDir 的 medium 设置为 Memory&#xff0c;可以确保 /dev/shm 正确地挂载到一个基于内存的文件系统&#xff0c;从而实现高效的共享内…

ubuntu创建虚拟环境安装ultralytics

安装Python和pip&#xff08;如果尚未安装&#xff09;: sudo apt update sudo apt install python3 python3-pip 安装virtualenv: sudo pip3 install virtualenv 创建虚拟环境: sudo virtualenv -p python3 myenv 这里myenv是虚拟环境的名称&#xff0c;-p python3指定使用…

【掌握 DDL】:SQL 中的数据库与表管理

掌握 DDL&#xff1a;SQL 中的数据库与表管理 掌握 DDL&#xff1a;SQL 中的数据库与表管理数据库 DDL创建数据库查看数据库查看所有数据库查看数据库创建语句 进入数据库删除数据库备份数据库备份恢复 查看数据库连接深入理解数据库创建与删除数据库字符集与校验规则 表 DLL创…

vscode docker 调试

目录 启动docker&#xff1a; vscode docker 调试 如果已经安装docker并且启动了。 启动docker&#xff1a; docker exec -it nlf /bin/bash vscode docker 调试 按照图中1 2 3 的顺序&#xff0c;进入&#xff0c;可以加载docker进行调试了。

HTML01:HTML基本结构

HTML基本结构 <html> <head><meta charset"UTF-8"><title>我的第一个网页</title> </head> <body>我的第一个网页 </body> </html><body、</body等成对的标签&#xff0c;分别叫开发标签和闭合标签单独…

URP - 屏幕图像(_CameraOpaqueTexture)

首先需要在unity中开启屏幕图像开关才可以使用该纹理 同样只有不透明对象才能被渲染到屏幕图像中 若想要该对象不被渲染到屏幕图像中&#xff0c;可以将其Shader的渲染队列改为 "Queue" "Transparent" 如何在Shader中使用_CameraOpaqueTexture&#xf…

如何在Ubuntu上安装NVIDIA显卡驱动?

作者&#xff1a;算力魔方创始人/英特尔创新大使刘力 一&#xff0c;前言 对于使用NVIDIA显卡的Ubuntu用户来说&#xff0c;正确安装显卡驱动是获得最佳图形性能的关键。与Windows系统不同&#xff0c;Linux系统通常不会自动安装专有显卡驱动。本文将详细介绍在Ubuntu系统上安…

机器视觉的手机FPC油墨丝印应用

在现代智能手机制造过程中&#xff0c;精密的组件装配和质量控制是确保产品性能和用户体验的关键。其中&#xff0c;柔性印刷电路板&#xff08;FPC&#xff09;的油墨丝印工艺尤为关键&#xff0c;它不仅影响到电路板的美观&#xff0c;更直接关系到电路的导电性能和可靠性。而…

Android智能体开发框架-架构文档

编写目的 1 提高智能体的开发效率&#xff0c; 2 降低系统开销&#xff0c; 3 支持跨平台扩展&#xff0c; 4 提供统一的开发范式 整体架构 接口层&#xff08;api层&#xff09;&#xff1a;提供API供开发者调用&#xff0c;支持Java/Kotlin和Native&#xff08;C&#x…

MySQL----数据库的操作

1. 查看数据库 语法&#xff1a;show databases; 示例展示&#xff1a; 2. 创建库 语法&#xff1a; CREATE DATABASE [IF NOT EXISTS] database_name[CHARACTER SET charset_name][COLLATE collation_name]; 注意&#xff1a;[] 为可选项 {} 为必选项 database_name 为数据…

两种方法求解最长公共子序列问题并输出所有解

最长公共子序列&#xff08;Longest Common Subsequence, LCS&#xff09;是动态规划领域的经典问题&#xff0c;广泛应用于生物信息学&#xff08;如DNA序列比对&#xff09;、文本差异比对&#xff08;如Git版本控制&#xff09;等领域。本文将通过​​自顶向下递归记忆化​​…

【Linux网络】网络协议基础

网络基础 计算机网络背景 独立模式:计算机之间相互独立 网络互联:多台计算机连接在一起,完成数据共享 局域网LAN:计算机数量更多了,通过交换机和路由器连接在一起 广域网WAN:将远隔千里的计算机都连在一起 所谓"局域网"和"广域网"只是一个相对的概念.比…

LeapVAD:通过认知感知和 Dual-Process 思维实现自动驾驶飞跃——论文阅读

《LeapVAD: A Leap in Autonomous Driving via Cognitive Perception and Dual-Process Thinking》2025年1月发表&#xff0c;来自浙江大学、上海AI实验室、慕尼黑工大、同济大学和中科大的论文。 尽管自动驾驶技术取得了显著进步&#xff0c;但由于推理能力有限&#xff0c;数…

windows 部署 Kafka3.x KRaft 模式 不依赖 ZooKeeper

1.下载 https://archive.apache.org/dist/kafka/3.9.0/kafka_2.12-3.9.0.tgz2.配置使用 KRaft 模式 2.1 修改 Kafka 的配置文件 cd D:\data\bigdata\kafka_2.12-3.9.0\config\kraft 修改 server.properties # 设置 Kafka 数据日志存储目录 log.dirsD:\\data\\bigdata\\kaf…