繁体   English   中英

RecyclerView notifyDataSetChanged()不起作用-Kotlin

[英]RecyclerView notifyDataSetChanged() not working - Kotlin

调用notifyDataSetChanged()后, RecyclerView没有更新,但我发现了问题,但没有解决。
这是我更改数据的代码。

fun changeData(data: List<AccountLite>) {
    // checking the size of the list before
    logI("accountLiteList.size before: " + accountLiteList.size)
    accountLiteList = data.toMutableList()

    // checking the size of the list after
    logI("accountLiteList.size after: " + accountLiteList.size)

    notifyDataSetChanged()
}

但是getItemCount()仍然返回0。

这是getItemCount()的代码。

override fun getItemCount(): Int {
    logI("getItemCount: " + accountLiteList.size)
    return accountLiteList.size
}

这是日志

AccountsRecyclerAdaptor: accountLiteList.size before: 0
AccountsRecyclerAdaptor: accountLiteList.size after: 19
AccountsRecyclerAdaptor: getItemCount: 0
AccountsRecyclerAdaptor: getItemCount: 0
...

为什么getItemCount()返回0,而accountLiteList没有在任何地方更改。
是完整的代码。

我认为您应该使用notifyItemChanged(position); 因为您要添加项目:

Int count=0;
for (item: AccountLite in data) 
{
   accountLiteList.add(item);
   notifyItemChanged(count);
   count++;
}

我试图使用AsyncTask调用API。 经过大量尝试,我找到了一种可行的解决方案,但不知道为什么。

我只是将inner关键字添加到Async类中,并且它起作用了。

例:

inner class ApiCall : AsyncTask

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM