Kotlin notifyDataSetChanged在片段中不起作用(带有ListView的BaseAdapter)

问题描述 投票:0回答:1

在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的父对象)调用]

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)',并且上下文不为null,并且已接收到所有数据。

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

!!!适配器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
    }

}
android kotlin adapter baseadapter notifydatasetchanged
1个回答
0
投票

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();
   }
 }
© www.soinside.com 2019 - 2024. All rights reserved.