Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setItems() always replace current items #48

Closed
HombreTech opened this issue May 9, 2020 · 1 comment
Closed

setItems() always replace current items #48

HombreTech opened this issue May 9, 2020 · 1 comment
Labels

Comments

@HombreTech
Copy link

adapter.setDiffCallback(ItemsDiffCallback())
adapter.setItems(projectsList.data)
class ItemsDiffCallback : DefaultDiffCallback<ViewModel>() {

    override fun areItemsTheSame(oldItem: ViewModel, newItem: ViewModel): Boolean {
        if (oldItem is ProjectsList.Data) {
            return if (newItem is ProjectsList.Data) {
                oldItem.id == newItem.id
            } else {
                false
            }
        } else if (newItem is ProjectsList.Data) {
            return false
        }
        return super.areItemsTheSame(oldItem, newItem)
    }

    override fun areContentsTheSame(oldItem: ViewModel, newItem: ViewModel): Boolean {
        return super.areContentsTheSame(oldItem, newItem)
    }

    override fun getChangePayload(oldItem: ViewModel, newItem: ViewModel): Any? {
        if (oldItem is ProjectsList.Data) {
            if (newItem is ProjectsList.Data) {
                val payload: ArrayList<Long> = ArrayList()
                payload.add(System.currentTimeMillis())
                return payload
            }
        }
        val payload = super.getChangePayload(oldItem, newItem)
        return payload
    }

}

Inside ProjectsList.Data:

override fun equals(obj: Any?): Boolean {
            if (obj is Data) {
                val pm: Data = obj
                return pm.id == this.id
            }
            return false
        }

So, always when i use setItems(projectsList.data) its replace my current list by projectsList.data (items various). Собственно, how to добавить элементы в список не перезаписав текущие?

@vivchar vivchar added the How To label May 10, 2020
@vivchar
Copy link
Owner

vivchar commented May 10, 2020

You should have your local list.

ArrayList<Data> localList = ArrayList<>();

public void update() {
    List<Data> partOfList = getPartOfList();
    localList.addAll(partOfList);
    adapter.setItems(localList)
}

@vivchar vivchar closed this as completed May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants