Search code examples
javajsonandroid-tvleanback

Beginner leanback tutorials showing how to display and update browsefragment data


I am trying to learn about developing android TV apps and looking at sample code on github and on some tutorial links. I have grasped some basics around android TV development.

My problem is non of the tutorials properly explain how the browse fragment is populated with data from an online json source and how it is updated.

Can anyone please direct me to a link or source that can be used as a decent tutorial for a beginner?


Solution

  • I don't think there are any tutorials specifically for LeanBack, but you can probably find plenty of general android tutorials on how to use Retrofit to fetch json formatted data from a public API.

    As for populating your BrowseFragment, something like this should do it:

    //Create a rows adapter for your fragment
    ArrayObjectAdapter mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
    setAdapter(mRowsAdapter);
    
    //Create a row and populate it
    ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(someItemPresenter);
    listRowAdapter.setItems(someItems, someDiffCallback);
    ListRow row = new ListRow(listRowAdapter);
    
    //Add row
    mRowsAdapter.add(row);
    

    someItems should be a List of your fetched items. someItemPresenter should be a class that extends Presenter and handles items of the type contained in the someItems list. someDiffCallback should be a DiffCallback.