有没有人让 notifyDataSetChanged() 在 Kotlin 工作?
Has anyone got notifyDataSetChanged() working in Kotlin?
我有一个 RecyclerView
,它显示从 MutableList
modSamplePhrases 读取的短语列表。当点击一个项目时,会弹出一个 DialogFragment
,显示列表项目中的短语并允许对其进行编辑。
编辑完成后,新文本通过函数 getDataFromDialog()
返回到 RecyclerFragment
。
Logcat 显示 基础数据更新成功 (因此 .clear()
和 .addAll()
没有区别)但仍然调用 notifyItemChanged(pos)
或notifyDataSetChanged()
什么都不做。
class PhraseRecyclerFragment : Fragment(){
private var modSamplePhrases = PhraseData().samplePhrases
private var phraseListAdapter = PhraseListAdapter(modSamplePhrases)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_recycler, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
list_recycler_view.apply {
layoutManager = LinearLayoutManager(activity)
adapter = phraseListAdapter
}
list_recycler_view.addOnItemClickListener(object: OnItemClickListener {
override fun onItemClicked(position: Int, view: View) {
val newFragment = PhraseDialogFragment()
val args = Bundle()
args.putInt("POSITION", position)
args.putString("CONTENT", modSamplePhrases[position].sample)
newFragment.arguments = args
val fm = fragmentManager
newFragment.show(fm, "STF")
}
})
}
fun getDataFromDialog(pos: Int, dialogText: String) {
modSamplePhrases[pos].sample = dialogText
println("item " + pos + ": " + modSamplePhrases[pos].sample)
phraseListAdapter.notifyDataSetChanged()
}
我试过修改数据后重新创建适配器
phraseListAdapter = PhraseListAdapter(modSamplePhrases)
我还尝试获取对 RecyclerView
的引用并重置其上的适配器
recyclerView?.adapter = phraseListAdapter
但还是没有。
PhraseListAdapter
代码:
class PhraseViewHolder(inflater: LayoutInflater, parent: ViewGroup) :
RecyclerView.ViewHolder(inflater.inflate(R.layout.list_item, parent, false)) {
private var indexView: TextView? = null
private var sampleView: TextView? = null
init {
indexView = itemView.findViewById(R.id.text_index)
sampleView = itemView.findViewById(R.id.text_sample)
}
fun bind(phrase: Phrase) {
indexView?.text = phrase.sample
sampleView?.text = phrase.index.toString()
}
}
class PhraseListAdapter (private val list: MutableList<Phrase>) : RecyclerView.Adapter<PhraseViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhraseViewHolder {
val inflater = LayoutInflater.from(parent.context)
return PhraseViewHolder(inflater, parent)
}
override fun onBindViewHolder(holder: PhraseViewHolder, position: Int) {
val phrase: Phrase = list[position]
holder.bind(phrase)
}
override fun getItemCount(): Int = list.size
}
´´´
捂脸。问题是我从片段外部调用 getDataFromDialog()
函数,如下所示:
PhraseRecyclerFragment().getDataFromDialog(pos, "" + textViewContent.text)
所以我需要将函数放在伴随对象中:
companion object {
var modSamplePhrases = PhraseData().samplePhrases
var phraseListAdapter = PhraseListAdapter(modSamplePhrases)
fun getDataFromDialog(pos: Int, dialogText: String) {
modSamplePhrases[pos].sample = dialogText
phraseListAdapter.notifyDataSetChanged()
}
能够调用它:
PhraseRecyclerFragment.getDataFromDialog(pos, "" + textViewContent.text)
现在一切正常。我的错。感谢所有试图提供帮助的人。
我有一个 RecyclerView
,它显示从 MutableList
modSamplePhrases 读取的短语列表。当点击一个项目时,会弹出一个 DialogFragment
,显示列表项目中的短语并允许对其进行编辑。
编辑完成后,新文本通过函数 getDataFromDialog()
返回到 RecyclerFragment
。
Logcat 显示 基础数据更新成功 (因此 .clear()
和 .addAll()
没有区别)但仍然调用 notifyItemChanged(pos)
或notifyDataSetChanged()
什么都不做。
class PhraseRecyclerFragment : Fragment(){
private var modSamplePhrases = PhraseData().samplePhrases
private var phraseListAdapter = PhraseListAdapter(modSamplePhrases)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_recycler, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
list_recycler_view.apply {
layoutManager = LinearLayoutManager(activity)
adapter = phraseListAdapter
}
list_recycler_view.addOnItemClickListener(object: OnItemClickListener {
override fun onItemClicked(position: Int, view: View) {
val newFragment = PhraseDialogFragment()
val args = Bundle()
args.putInt("POSITION", position)
args.putString("CONTENT", modSamplePhrases[position].sample)
newFragment.arguments = args
val fm = fragmentManager
newFragment.show(fm, "STF")
}
})
}
fun getDataFromDialog(pos: Int, dialogText: String) {
modSamplePhrases[pos].sample = dialogText
println("item " + pos + ": " + modSamplePhrases[pos].sample)
phraseListAdapter.notifyDataSetChanged()
}
我试过修改数据后重新创建适配器
phraseListAdapter = PhraseListAdapter(modSamplePhrases)
我还尝试获取对 RecyclerView
的引用并重置其上的适配器
recyclerView?.adapter = phraseListAdapter
但还是没有。
PhraseListAdapter
代码:
class PhraseViewHolder(inflater: LayoutInflater, parent: ViewGroup) :
RecyclerView.ViewHolder(inflater.inflate(R.layout.list_item, parent, false)) {
private var indexView: TextView? = null
private var sampleView: TextView? = null
init {
indexView = itemView.findViewById(R.id.text_index)
sampleView = itemView.findViewById(R.id.text_sample)
}
fun bind(phrase: Phrase) {
indexView?.text = phrase.sample
sampleView?.text = phrase.index.toString()
}
}
class PhraseListAdapter (private val list: MutableList<Phrase>) : RecyclerView.Adapter<PhraseViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhraseViewHolder {
val inflater = LayoutInflater.from(parent.context)
return PhraseViewHolder(inflater, parent)
}
override fun onBindViewHolder(holder: PhraseViewHolder, position: Int) {
val phrase: Phrase = list[position]
holder.bind(phrase)
}
override fun getItemCount(): Int = list.size
}
´´´
捂脸。问题是我从片段外部调用 getDataFromDialog()
函数,如下所示:
PhraseRecyclerFragment().getDataFromDialog(pos, "" + textViewContent.text)
所以我需要将函数放在伴随对象中:
companion object {
var modSamplePhrases = PhraseData().samplePhrases
var phraseListAdapter = PhraseListAdapter(modSamplePhrases)
fun getDataFromDialog(pos: Int, dialogText: String) {
modSamplePhrases[pos].sample = dialogText
phraseListAdapter.notifyDataSetChanged()
}
能够调用它:
PhraseRecyclerFragment.getDataFromDialog(pos, "" + textViewContent.text)
现在一切正常。我的错。感谢所有试图提供帮助的人。