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

时间:2019-10-31 08:53:30

标签: list kotlin recycler-adapter

我从一个活动向适配器传递了一个列表,并在调用 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())
    }
}

//////设置适配器。

  private var adapter: StationsListAdapter = StationsListAdapter(this)
   recycler_view_stationsList.apply {

            layoutManager = LinearLayoutManager(this.context)
            this.adapter = adapter
        }

//////布局文件

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_stationsList"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        />

//。主要活动完整代码,我使用了inject来调用presenter,主要问题在于setRecycler函数

 @Inject
    lateinit var stationsListPresenter: StationsListPresenter
    private lateinit var station_adapter: StationsListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stations_list)
       setupRecycler()
        stationsListPresenter.downLoadStationsListUsingRetrofit()
    }

    private fun setupRecycler()
    {
        val recyclerView : RecyclerView = findViewById(R.id.recycler_view_stations_list)
        recyclerView.apply {
            layoutManager = LinearLayoutManager(this@StationsListActivity) // replace MainActivity with your activity name
            station_adapter = StationsListAdapter(this@StationsListActivity)
        }

            recyclerView.adapter = station_adapter

    }


    override fun addNewsToRecyclerView(stationsList: StationsListDataClass)
    {
        station_adapter.add_data(stationsList)


    }
    override fun makeInjection(activityComponent: ActivityComponent) {

        activityComponent.inject(this)
    }

调用notifydatasetchanged()后,视图仍然为空 而且我没有任何错误。 该列表有大约800个对象。 即使使用findviewbyID绑定视图之后,Recycler View仍显示空指针异常

2 个答案:

答案 0 :(得分:0)

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

recyclerView.setLayoutManager(new LinearLayoutManager(this));

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

list.clear()
list.addAll(..)

答案 1 :(得分:0)

并像这样修改add_data函数

fun add_data(data : StationsListDataClass){
     list.clear()
     list.addAll(data.stationBeanList)
     notifyDataSetChanged()
     Log.d("List", list.toString())
}

更新

按如下所示修改您的setupRecycler

private fun setupRecycler(){
    var recyclerView = findViewById(R.id.recycler_view_stations_list)

    station_adapter = StationsListAdapter(this@StationsListActivity)
    val layoutManager = LinearLayoutManager(this@StationsListActivity)

    recyclerView?.layoutManager = layoutManager
    recyclerView?.adapter = station_adapter
}