我试图将我的适配器转换为ListAdapter,以便将来包含分页库。
我的应用程序是一个基本的2个片段注释应用程序,第一个片段具有回收站 View ,第二个片段具有EditText以便将文本插入到回收站 View 。
我已经实现了所有内容,由于某种原因,在调用submitList()之后,我的recyclerview不显示任何内容。
我观察到我的实时数据列表和适配器列表,而我的实时数据列表是从房间数据库中获取数据,然后我向其中查询数据。在我将适配器转换为ListAdapter之前,一切正常
有什么建议么?
我的适配器类:
package com.example.mvvm_example
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.mvvm_example.db.Word
interface OnItemClickListener : View.OnClickListener {
override fun onClick(v: View?) {
}
}
class ListAdapterCallBack : DiffUtil.ItemCallback<Word>() {
override fun areItemsTheSame(oldItem: Word, newItem: Word): Boolean {
return oldItem.mWord == newItem.mWord
}
override fun areContentsTheSame(oldItem: Word, newItem: Word): Boolean {
return oldItem == newItem
}
}
class WordAdapter(
private val context: Context,
private val onItemClickListener: OnItemClickListener
) :
ListAdapter<Word, WordAdapter.WordViewHolder>(ListAdapterCallBack()) {
private var wordList = listOf<Word>()
class WordViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
private val textView = view.findViewById<TextView>(R.id.textView)!!
fun onBind(listener: OnItemClickListener, word: Word) {
textView.text = word.getmWord()
view.setOnClickListener {
listener.onClick(view)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
val inflater = LayoutInflater.from(context)
return WordViewHolder(inflater.inflate(R.layout.recyclerview_item, parent, false))
}
override fun getItemCount(): Int {
return wordList.size
}
fun setWords(list: MutableList<Word>) {
wordList = list
notifyDataSetChanged()
}
override fun submitList(list: MutableList<Word>?) {
Log.d("Adapter", "list sumbitted list is ${list.toString()}")
super.submitList(list?.let { ArrayList(it) })
}
override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
val word = wordList[position]
holder.onBind(onItemClickListener, word)
}
}
我的第一个片段包含了recyclerview:
package com.example.mvvm_example
import android.app.Activity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.mvvm_example.db.Word
import kotlinx.android.synthetic.main.fragment_first.*
private const val TAG = "firstFrag"
class FirstFragment : Fragment() {
private lateinit var viewModel: AppViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
Log.d(TAG, "onCreateView called")
return inflater.inflate(R.layout.fragment_first, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
Log.d(TAG, "onActivityCreated called")
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(AppViewModel::class.java)
val recyclerView = requireActivity().findViewById<RecyclerView>(R.id.recyclerview)
val adapter =
WordAdapter(requireActivity().applicationContext, object : OnItemClickListener {
override fun onClick(v: View?) {
super.onClick(v)
Toast.makeText(
requireActivity().applicationContext,
"item clicked",
Toast.LENGTH_SHORT
).show()
}
})
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
viewModel.getAllWords().observe(requireActivity(), Observer {
adapter.submitList(it)
})
fab.setOnClickListener {
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
}
delfab.setOnClickListener {
viewModel.deleteAllWords()
Toast.makeText(requireContext(), "Words Deleted", Toast.LENGTH_LONG).show()
}
}
}
我的Word数据类:
package com.example.mvvm_example.db
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import org.jetbrains.annotations.NotNull
@Entity(tableName = "word_table")
data class Word(
@ColumnInfo(name = "word")
@PrimaryKey
@NotNull
val mWord: String
) {
fun getmWord() = mWord
}
编辑
如果我用
adapter.submitList(it)
切换adapter.setWords(it)
也可以..所以该submitList()
方法一定有问题 最佳答案
如果使用ListAdapter
,则意味着它本身管理列表项。您应该删除内部的wordList
和方法getItemCount
。对于访问数据,仅调用getItem(position)
方法。无需覆盖方法submitList