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

相关内容

热门资讯

玩家交流!hhpoker真能买... 玩家交流!hhpoker真能买到挂吗,wepoker透视有吗,详细教程(有挂透视);支持2-10人实...
透视游戏!hhpoker德州有... 透视游戏!hhpoker德州有挂吗(透视)本来存在有挂(详细辅助安装教程);1、任何ai辅助神器的玩...
实测分享!黑侠破解wepoke... 实测分享!黑侠破解wepoker,aapoker插件下载,细节揭秘(有挂方法)1、许多玩家不知道aa...
透视好友房!智星德州可以透视吗... 透视好友房!智星德州可以透视吗(透视)确实是真的有挂(详细辅助大神讲解)1、进入到黑科技之后,能看到...
最新技巧!hhpoker怎么防... 1、最新技巧!hhpoker怎么防作弊,德普之星辅助软件,第三方教程(有挂方法)。2、德普之星辅助软...
透视app!来玩app破解版(... 透视app!来玩app破解版(透视)真是真的是有挂(详细辅助黑科技教程)1、系统规律教程、辅助透视等...
交流学习经验!约局吧app有挂... 交流学习经验!约局吧app有挂吗,wepoker永久免费脚本,可靠技巧(有挂辅助);人气非常高,ai...
透视新版!wepoker透视脚... 透视新版!wepoker透视脚本(透视)原来是真的有挂(详细辅助微扑克教程)1、点击下载安装,wep...
指导大家!aapoker发牌逻... 指导大家!aapoker发牌逻辑,哈糖大菠萝软件下载,细节方法(有挂技巧)准备好在哈糖大菠萝软件下载...
透视辅助!德扑圈透视(透视)一... 透视辅助!德扑圈透视(透视)一直真的有挂(详细辅助扑克教程)1、完成的残局,帮助玩家取得所有比赛的胜...