回收站适配器在调用notifydatasetchanged()时未更新

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

我从一个活动向适配器传递了一个列表,并在调用notifydatasetchanged时,回收者视图仍然为空,它没有更新该视图。

var list : ArrayList<StationBean> = ArrayList()
override fun getItemCount(): Int {
    return list.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return ViewHolder(LayoutInflater.from(context).inflate(R.layout.stations_adapter, parent, false))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.stationsName.text = list.get(position).stationName
    holder.cityName.text = list.get(position).latitude.toString()
}
fun add_data(data : StationsListDataClass)
{
       list = data.stationBeanList
       notifyDataSetChanged()
       Log.d("List", list.toString())
}

}

在调用notifydatasetchanged()后,视图仍然为空而且我没有任何错误。该列表包含约800个对象。

list kotlin recycler-adapter
1个回答
0
投票

请发布您的xml布局和/或设置回收者视图代码。也许没有设置LayoutManager。您可以在xml或活动/片段中进行设置:

recyclerView.setLayoutManager(new LinearLayoutManager(this));

也尝试清除add_data()中的列表

list.clear()
list.addAll(..)
© www.soinside.com 2019 - 2024. All rights reserved.