android tv -Reloading adapter data
Asked Answered
S

5

8

I want to re-load some of the rows data in a browse fragment.

Basically I want to reset the adapter data without causing a flash like effect in the browse fragment. Any idea how it can be done?

Something like notifyDataSetChanged() in list view.

Thanx

Swordplay answered 23/11, 2015 at 11:32 Comment(1)
just add the values to the adapter using adapter.add(item). incase you are adding to existing values or do adapter.clear() and the add.Slashing
G
5

This will refresh data without losing current position:

for (int i = 0; i < mAdapter.size(); i++) {
    ListRow listRow = ((ListRow) mAdapter.get(i));
    ArrayObjectAdapter listRowAdapter = ((ArrayObjectAdapter) listRow.getAdapter());
    if (listRowAdapter.size() > 0) {
        listRowAdapter.notifyArrayItemRangeChanged(0, listRowAdapter.size());
    }
}
Grazing answered 3/1, 2017 at 13:28 Comment(0)
D
1

I answered something similar in this question: Dynamically add more cards in a list Row android TV leanback

I'll copy my answer from there. Hopefully this helps.

To add items to a presenter;

CardPresenter channelCardPresenter = new CardPresenter();
ArrayObjectAdapter channelRowAdapter = new ArrayObjectAdapter(channelCardPresenter);
channelRowAdapter.add(new Movie("Movie Title"));
HeaderItem header = new HeaderItem(0, "My Channels");
mRowsAdapter.add(new ListRow(header, channelRowAdapter));

Of course this may only work for the first time you're creating the UI. Future times may not do this. In my app, CumulusTV, I create a method that will be called each time I want to do a full redraw of the app:

public void refreshUI() {
    prepareBackgroundManager();
    setupUIElements();
    loadRows(); //Generate and populate all the rows
    setupEventListeners();
}
Duster answered 23/11, 2015 at 18:24 Comment(2)
your link is broken, can you please post it again?Ihs
I have made many changes to the codebase since I originally posted the answer. I went back through the commit history and updated the URL to point to the file at the time of answering.Duster
C
1

You can create your own adapter and add a new replaceAll() method.

public void replaceAll(Collection items){
 int itemsCount = items.size();
 if (itemsCount == 0){
    return;
 }
 mItems.clear();
 mItems.addAll(index, items);
 notifyItemRangeChanged(0, itemsCount);
}

complete source:link

Crucify answered 27/7, 2016 at 17:59 Comment(0)
S
0

Here's my kotlin version of @ewilly's answer, implementing only replaceAll which I use to both initially populate and incrementally update the list.

class RefreshableArrayObjectAdapter(presenter: Presenter): ObjectAdapter(presenter) {
    var items: ArrayList<Any> = arrayListOf()

    override fun isImmediateNotifySupported(): Boolean = true

    override fun size(): Int = items.size

    override fun get(position: Int): Any = items[position]

    fun replaceAll(list: Collection<Any>) {
        items.clear()
        items.addAll(0, list)
        notifyChanged()
    }
}
Sidesaddle answered 13/6, 2017 at 1:17 Comment(0)
P
0
  /**
 * Notify that the content of a range of items changed. Note that this is
 * not same as items being added or removed.
 *
 * @param positionStart The position of first item that has changed.
 * @param itemCount The count of how many items have changed.
 */
public void notifyArrayItemRangeChanged(int positionStart, int itemCount) {
    notifyItemRangeChanged(positionStart, itemCount);
}

Notify that the content of a range of items changed. Note that this is * not same as items being added or removed.

if you want add or remove item,please try this:

1.mRowsAdapter.clear();
2.mRowsApdapter.addAll();
Potentate answered 25/12, 2017 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.