Android单词示例(MVVM,LiveData,Room)+ SearchView + 删除,删除错误项
创始人
2024-10-06 23:01:41
0

以下是一个简单的示例,展示了如何使用MVVM架构,LiveData,Room和SearchView来实现一个带有删除功能的Android应用。

首先,我们需要添加所需的依赖项到项目的build.gradle文件中:

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.room:room-runtime:2.3.0'
implementation 'androidx.room:room-ktx:2.3.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'

接下来,我们需要创建一个数据实体类,表示我们要在应用中存储和显示的单词:

@Entity(tableName = "word_table")
data class Word(
    @PrimaryKey(autoGenerate = true)
    val id: Long = 0,
    val word: String
)

然后,我们需要创建一个用于访问数据库的数据访问对象(DAO):

@Dao
interface WordDao {
    @Query("SELECT * FROM word_table WHERE word LIKE :searchQuery")
    fun searchWords(searchQuery: String): LiveData>

    @Insert
    suspend fun insertWord(word: Word)

    @Delete
    suspend fun deleteWord(word: Word)
}

接下来,我们需要创建一个用于管理数据的Repository类:

class WordRepository(private val wordDao: WordDao) {
    val allWords: LiveData> = wordDao.getAllWords()

    suspend fun insert(word: Word) {
        wordDao.insertWord(word)
    }

    suspend fun delete(word: Word) {
        wordDao.deleteWord(word)
    }

    fun searchWords(searchQuery: String): LiveData> {
        return wordDao.searchWords("%$searchQuery%")
    }
}

然后,我们需要创建一个ViewModel类,负责从Repository获取数据并在需要时更新UI:

class WordViewModel(private val repository: WordRepository) : ViewModel() {
    private val _searchQuery = MutableLiveData()

    val searchQuery: LiveData
        get() = _searchQuery

    val searchResults: LiveData> = Transformations.switchMap(_searchQuery) {
        repository.searchWords(it)
    }

    fun setSearchQuery(query: String) {
        _searchQuery.value = query
    }

    fun deleteWord(word: Word) {
        viewModelScope.launch {
            repository.delete(word)
        }
    }
}

接下来,我们需要创建一个适配器类来显示单词列表:

class WordAdapter(private val onWordDelete: (Word) -> Unit) :
    RecyclerView.Adapter() {

    private var words: List = emptyList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_word, parent, false)
        return WordViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
        val currentWord = words[position]
        holder.bind(currentWord)
    }

    override fun getItemCount(): Int {
        return words.size
    }

    fun setWords(words: List) {
        this.words = words
        notifyDataSetChanged()
    }

    inner class WordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val wordTextView: TextView = itemView.findViewById(R.id.wordTextView)
        private val deleteButton: ImageButton = itemView.findViewById(R.id.deleteButton)

        fun bind(word: Word) {
            wordTextView.text = word.word

            deleteButton.setOnClickListener {
                onWordDelete(word)
            }
        }
    }
}

接下来,我们需要在Activity或Fragment中使用这些组件来构建应用程序。在这个示例中,我们将在Fragment中实现:

class WordFragment : Fragment() {
    private lateinit var wordViewModel: WordViewModel
    private lateinit var wordAdapter: WordAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view = inflater.inflate(R.layout.fragment_word, container, false)

        val searchView: SearchView = view.findViewById(R.id.searchView)
        val recyclerView: RecyclerView = view.findViewById(R.id.recyclerView)

        wordAdapter = WordAdapter { word ->
            wordViewModel.deleteWord(word)
        }
        recyclerView.adapter

相关内容

热门资讯

透视妙招!wepoker提高好... 透视妙招!wepoker提高好牌率(WePoKer辅助挂)一贯真的是有辅助技巧(哔哩哔哩)1、下载好...
透视肯定!微信微乐游戏辅助脚本... 透视肯定!微信微乐游戏辅助脚本,微信微乐辅助器使用教程(透视)都是真的是有辅助挂(哔哩哔哩)一、可以...
透视模板!hhpoker到底可... 您好,hhpoker到底可以作必弊码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704...
现有说明如下!微乐小程序黑科技... 现有说明如下!微乐小程序黑科技,微乐自建房辅助工具(透视)都是存在有辅助工具(哔哩哔哩)1、进入到是...
透视透视!微乐小程序黑科技,微... 透视透视!微乐小程序黑科技,微乐自建房免费辅助入口在哪里(透视)其实真的有辅助攻略(哔哩哔哩)1、辅...
透视阶段!wepoker透视破... 透视阶段!wepoker透视破解版(HHpoker机器人)真是真的是有辅助软件(哔哩哔哩)1、操作简...
透视策略!aapoker怎么选... 透视策略!aapoker怎么选牌(WePoKer脚本)原来是真的有辅助软件(哔哩哔哩)1、aapok...
出乎意料的是!微乐小程序黑科技... 出乎意料的是!微乐小程序黑科技免费,微信小程序微乐为什么老输(透视)一贯是真的辅助app(哔哩哔哩)...
复盘辅助挂!微信微乐游戏辅助脚... 复盘辅助挂!微信微乐游戏辅助脚本,微乐江西小程序辅助器免费(透视)真是存在有辅助技巧(哔哩哔哩)1、...
透视秘籍!wepoker轻量版... 透视秘籍!wepoker轻量版透视系统(WePoKer透视脚本)其实真的是有辅助攻略(哔哩哔哩)1、...