【问题标题】:Kotlin notifyDataSetChanged not work (BaseAdapter with ListView) in FragmentKotlin notifyDataSetChanged 在 Fragment 中不起作用(带有 ListView 的 BaseAdapter)
【发布时间】:2019-12-02 12:45:58
【问题描述】:

在 ListView 中使用 BaseAdapter 时,notifyDataSetChanged 不起作用。

代码如下。

class AFragment(val mContext: Context, b: ArrayList<CustomData>) : Fragment() {
var a: ArrayList<CustomData> = ArrayList<CustomData>()

init{ 
   a.clear()
   a.addAll(b)
}

下面的代码是Fragment中的setListView(这段代码被Activity(AFragment的parents)调用)

fun setListView(c: ArrayList<CustomData>) {
    a.clear()
    a.addAll(c)
    mAdapter.notifyDataSetChanged(a)
}

下面的代码是CustomAdapter。

class CustomerAdapter(val context: Context?, d: ArrayList<CustomData>) : BaseAdapter() {
    var z: ArrayList<CustomData> = d

    fun notifyDataSetChanged(f: ArrayList<CustomData>) {
        z.clear()
        z.addAll(f)
        notifyDataSetChanged()
    }

还确认适配器内的'notifyDataSetChanged(arrayList)'调用正常,上下文不为空,数据全部接收完毕。

为什么...为什么它不起作用...?请帮帮我.....;(

!!!适配器 notifyDataSetChanged 不起作用。所以.. ArrayList 已更改但未反映在 ListView 中。

class CustomAdapter(val context: Context?, arrayList: ArrayList<CustomAdapter>) : BaseAdapter() {
    var mArrayList: ArrayList<CustomAdapter> = arrayTrt

    fun notifyDataSetChanged(arrayList: ArrayList<CustomData>) {
        mArrayList.clear()
        mArrayList.addAll(arrayList)
        notifyDataSetChanged()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val view = LayoutInflater.from(context)....
        return view
    }

    override fun getItem(position: Int): Any {
        return mArrayList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return mArrayList.size
    }

}

【问题讨论】:

  • 你从来没有说过什么是工作。您希望发生什么没有发生的事情?
  • Holly... notifyDataSetChaged() 不起作用... ;( ... 适配器未刷新...!
  • 请为您的适配器添加完整的代码。
  • 我粘贴了适配器
  • 你有很多不同的ArrayList 变量——mArrayListFragmentmArrayTrtaArrayListarrayListmArrayListAdapter 中——很可能有些混淆了,指向同一个对象,清除一个你认为没有被清除的对象,等等。我们可能需要minimal reproducible example 来解决这个问题,包括你如何实例化@987654334 @,然后从 Activity 刷新它。

标签: android kotlin adapter baseadapter notifydatasetchanged


【解决方案1】:

ArrayList 发生更改后,您必须调用 notifyDataSetChanged() 以使用更改更新您的 ListView。这可以在适配器内部或外部完成。所以,对于
例子: 公共类 MyCustomBaseAdapter 扩展 BaseAdapter { //提示:不要让这个静态的,这只是一个坏主意 私有ArrayList searchArrayList;

     private LayoutInflater mInflater;

    public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults)     
    {
    searchArrayList = initialResults;
    mInflater = LayoutInflater.from(context);
     }

   public void updateResults(ArrayList<Contact> results) {
    searchArrayList = results;
    //Triggers the list update
    notifyDataSetChanged();
   }
 }

【讨论】:

  • 我是适配器内部的 notifyDataSetChanged。和 updateResult 一样,我在适配器内部调用 notifyDataSetChanged(ArrayList) 和 notifyDataSetChanged()。
猜你喜欢
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 2015-11-20
相关资源
最近更新 更多