0

我有一个显示从modSamplePhrasesRecyclerView读取的短语列表。当一个项目被点击时,会弹出一个显示列表项目中的短语并允许对其进行编辑的窗口。编辑完成后,通过函数将新文本返回给。Logcat 显示底层数据更新成功(所以没有区别),但仍然调用或根本不执行任何操作。MutableList DialogFragmentRecyclerFragmentgetDataFromDialog().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

}

´´´
4

1 回答 1

0

掌心。问题是我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)

现在一切正常。我的错。感谢所有试图提供帮助的人。

于 2020-02-05T21:17:59.283 回答