如何在选项卡式适配器上调用 notifyDatasetChanged?

How do I call notifyDatasetChanged on Tabbed Adapter?

我正在使用 OkHttpClient 使用 FragmentStatePagerAdapter 填充 3 个选项卡(我知道,已弃用)-- 这有效,但是当我将另一个 activity 和 return 中的数据更改为 MainActivity RecyclerView 没有更新,尽管我尝试在我能想到的任何地方调用 notifyDataSetChanged() 。这个问题以各种形式被问过无数次,但 none 的答案让我无所适从。

我的 MainActivity 是:

    private lateinit var viewPager: ViewPager
    private var InventoryItems: MutableList<MutableList<String>> = ArrayList()

    class MainActivity() : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            viewPager = findViewById(R.id.view_pager)

            callServer()
        }

        private fun callServer(){
            ...
                        override fun onResponse(call: Call, response: Response){
                    response.use {
                        if (!response.isSuccessful) throw IOException("Unexpected code $response")
                        val resp = response.body!!.string()
                        this@MainActivity.runOnUiThread(Runnable {
                            parseJsonStr(resp)//loads InventoryItems array from Json response
                                //logging server response shows data in array is changed
                            setTabs()
                        })
                    }
                }
        }

        private fun setTabs(){
            pagerAdapter = SectionsPagerAdapter(supportFragmentManager, InventoryItems, this@MainActivity)
            viewPager.adapter = pagerAdapter
            val tabs: TabLayout = findViewById(R.id.tabs)
            tabs.setupWithViewPager(viewPager)
            pagerAdapter.notifyDataSetChanged()//(...doesn't work here, or at the top of this function)
        }
    }

SectionsPagerAdapter 是:

    private var InventoryTab1: MutableList<MutableList<String>> = ArrayList()//and same for Tab 2 and 3

    class SectionsPagerAdapter(
        fm: FragmentManager,
        private val invItems: MutableList<MutableList<String>>,
        private val context: Context
    ): FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        override fun getItem(position: Int): Fragment {
            ...//load tab arrays from invItems
            return when (position) {
                0 -> FirstFragment(InventoryTab1)
                1 -> SecondFragment(InventoryTab2)
                else -> ThirdFragment(InventoryTab3)
            }
        }
        override fun getItemPosition(`object`: Any): Int {
            return POSITION_NONE//tried this because of some SO answers
        }
    }

...片段是:

        private lateinit var rootView: View
        private lateinit var recyclerView: RecyclerView
    class FirstFragment(private val items: MutableList<MutableList<String>>) : Fragment() {
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            rootView = inflater.inflate(R.layout.fragment_first, container, false)
            recyclerView = rootView.findViewById(R.id.bk_rv)
            recyclerView.layoutManager = LinearLayoutManager(activity)
        fun clickListener(position: Int) {
            //intent to start other activity that changes server data
            startActivity(intent)
        }
        val listener = { i: Int -> clickListener(i) }
        val inventoryAdapter = activity?.let { InventoryAdapter( items, it, listener ) }
        recyclerView.adapter = inventoryAdapter
        recyclerView.adapter?.notifyDataSetChanged()//(...doesn't work here either)

        return rootView
    }

我试过将 notifyDataSetChanged() 放在多个位置的适配器上,但无济于事。记录初始数组显示 MainActivity 中的数据已更改,但这不会传递给 Fragments。

如有任何建议,我们将不胜感激!

终于用

解决了这个问题
startActivityForResult(intent, 1)

在我的片段中(很多 SO 问题都解决了这个问题,很好的教程在 this link);然后

    val intent = Intent()
    intent.putExtra("index", rowNumStr)
    intent.putExtra("newValue", newValueStr)
    setResult(Activity.RESULT_OK, intent)
    finish()

在向服务器提交更改的activity中;回到片段中:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                val indexStr = data?.getStringExtra("index")
                val valueStr = data?.getStringExtra("newValue")
                items[indexStr!!.toInt()][5] = valueStr.toString()
                recyclerView.adapter?.notifyDataSetChanged()
            }
        }
    }

希望这对某人有所帮助!