简体   繁体   中英

RecyclerView notifyDataSetChanged() not working - Kotlin

RecyclerView not update after calling notifyDataSetChanged() and I found the problem but not solution.
Here is my code of changing the data.

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()
}

But getItemCount() still return 0.

Here is the code of getItemCount() .

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

And here is the log

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

Why the getItemCount() return 0, and the accountLiteList not changed anywhere.
Here is the complete code.

I think you should use notifyItemChanged(position); because you're adding items:

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

I was trying to call API using AsyncTask. After lot of tries, I found one solution which worked, but don't know why.

I just added the inner keyword to the Async class, and it worked.

Example:

inner class ApiCall : AsyncTask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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