Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 有人在Kotlin工作过notifyDataSetChanged()吗?_Android_Kotlin_Android Recyclerview_Notifydatasetchanged_Notifyitemchanged - Fatal编程技术网

Android 有人在Kotlin工作过notifyDataSetChanged()吗?

Android 有人在Kotlin工作过notifyDataSetChanged()吗?,android,kotlin,android-recyclerview,notifydatasetchanged,notifyitemchanged,Android,Kotlin,Android Recyclerview,Notifydatasetchanged,Notifyitemchanged,我有一个RecyclerView,它显示了从MutableListmodSamplePhrases读取的短短语列表。点击某个项目时,会弹出一个对话框片段,显示列表项目中的短语并允许对其进行编辑。 编辑完成后,新文本将通过函数getDataFromDialog()返回到RecyclerFragment。 Logcat显示,基础数据成功更新(因此.clear()&.addAll()没有区别),但对notifyItemChanged(pos)或notifyDataSetChanged()的调用仍然没有

我有一个
RecyclerView
,它显示了从
MutableList
modSamplePhrases读取的短短语列表。点击某个项目时,会弹出一个对话框片段,显示列表项目中的短语并允许对其进行编辑。 编辑完成后,新文本将通过函数
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
code:

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

}

´´´
类短语视图持有者(充气机:布局充气机,父级:视图组):
RecyclerView.ViewHolder(充气机充气(R.layout.list_项,父项,假)){
私有变量indexView:TextView?=null
私有变量sampleView:TextView?=null
初始化{
indexView=itemView.findViewById(R.id.text\u索引)
sampleView=itemView.findViewById(R.id.text\u示例)
}
有趣的绑定(短语:短语){
indexView?.text=短语.sample
sampleView?.text=phrase.index.toString()
}
}
类PhraseListAdapter(私有值列表:MutableList):RecyclerView.Adapter(){
重写CreateViewHolder(父级:ViewGroup,viewType:Int):短语ViewHolder{
val inflater=LayoutInflater.from(parent.context)
返回短语视图保持架(充气机、父级)
}
覆盖BindViewHolder(holder:PhraseViewHolder,位置:Int){
val短语:短语=列表[位置]
绑定(短语)
}
重写getItemCount():Int=list.size
}
´´´

Facepalm。问题是我从片段外部调用了
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指向旧的istance,因此显然通知新实例不会做任何事情,就像@gpunto指出的那样,您正在重新创建适配器实例,但没有将其设置为recyclerview。您可以将其设置为新的,并省略notifydatasetchanged()调用。您可以尝试在适配器中使用update方法,在将数据添加到数据列表后,调用update方法并在其中调用notifiydatasetchanged()。我认为这应该行得通。我明白重新创建适配器的要点。我想我把那条线留在柱子上弄脏了水,所以我把它移走了。我还尝试在RecyclerView上重置适配器,以及同时重置两个选项,但实际上,这两个选项都不是必需的。我还尝试将update方法放入适配器中,但结果相同-无。尽管进行了广泛的搜索,但我还没有在Kotlin anywhere中找到notifyDatasetChanged()的工作示例。