• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Unable to refresh BrowseFragment listrow successfully after making change to json data

darth_daniel

Lurker
Jan 24, 2019
7
0
Hi,

I am building an android tv app with a BrowseFragment that displays a listrow with data pulled from a php created JSON file (data is got from MySQL table). There is a check that is run to see if any changes to the database and after which when the change is detected, the BrowseFragment is to be refreshed reloading the json data.

I attempted the refresh from both the BrowseFragments parent activity and the fragment itself (I attempted to use the clear() and addall() functions of ArrayObjectAdapter) and there would just be a flash/blinking but the information would not be refreshed. The only way I have been able to update the listrow has been to restart the app.

How can I successfully update/refresh the BrowseFragment list row?

I have placed the code for my MainFragment and BrowseFragment

Java:
public class MainFragment extends BrowseSupportFragment
        implements LoaderManager.LoaderCallbacks<LinkedHashMap<String, List<Video>>>{

    private static final int BACKGROUND_UPDATE_DELAY = 300;

    private static final String TAG = MainFragment.class.getSimpleName();

    static final int GRID_ITEM_WIDTH = 300;
    private static final int GRID_ITEM_HEIGHT = 200;
    private final Handler mHandler = new Handler();
    //private ArrayObjectAdapter mRowsAdapter;
    private CustumArrayObjectAdapter mRowsAdapter;
    private Drawable mDefaultBackground;
    private DisplayMetrics mMetrics;
    private Runnable mBackgroundTask;
    private Uri mBackgroundURI;
    private BackgroundManager mBackgroundManager;
    private CustomListRow mGridItemListRow;
    private LoaderManager mLoaderManager;

    ArrayList<Video> mItems = null;
    private ArrayList<CustomListRow> mVideoListRowArray;
    private static final int VIDEO_ITEM_LOADER_ID = 1;

/**
    // Init
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            updateChannels();
            handler.postDelayed(this, 10000);
        }
    };

**/






    @Override
    public void onActivityCreated(Bundle savedInstanceState) {


        // Final initialization, modifying UI elements.
        super.onActivityCreated(savedInstanceState);

        // Prepare the manager that maintains the same background image between activities.
        prepareBackgroundManager();

        setupUIElements();

        loadRows();
        setRows();

        mBackgroundURI = Uri.parse("http://192.168.0.4/test/images/bkground/background1.jpg");

;
        mLoaderManager = LoaderManager.getInstance(this);
        mLoaderManager.initLoader(VIDEO_ITEM_LOADER_ID, null, this);

        setupEventListeners();
        prepareEntranceTransition();

        updateRecommendations();


        /**
        //Start
        handler.postDelayed(runnable, 10000);

         **/


    }

    @Override
    public void onDestroy() {
        mHandler.removeCallbacks(mBackgroundTask);
        mBackgroundManager = null;
        super.onDestroy();
    }

    /**

    public void updateChannels(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiInterface.JSONURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        ApiInterface api = retrofit.create(ApiInterface.class);

        Call<String> call = api.getString();

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.i("Responsestring", response.body().toString());
                //Toast.makeText()
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        Log.i("onSuccess", response.body().toString());

                        String jsonresponse = response.body().toString();
                        chkResponse(jsonresponse);

                    } else {
                        Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }

    private void chkResponse(String response){

        try {
            //getting the whole json object from the response
            JSONObject obj = new JSONObject(response);
            if(obj.optString("update").equals("true"))
            {

                try {


     getLoaderManager().restartLoader(0, null, this);


     mVideoListRowArray = null;
     mItems = null;



     mLoaderManager.restartLoader(0, null,  this);

     mLoaderManager = LoaderManager.getInstance(this);
     mLoaderManager.initLoader(VIDEO_ITEM_LOADER_ID, null, this);

     setupUIElements();
     loadRows();
     setRows();

     mRowsAdapter.clear();
     mRowsAdapter.addAll(0, mVideoListRowArray);
     mRowsAdapter.replaceAll(0, mVideoListRowArray);


     //Intent intent = new Intent(getActivity(), MainActivity.class);
     //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
     //startActivity(intent);

                } catch (Exception e)
                {
                    Toast.makeText(this, "Error Refreshing Channel Rows.", Toast.LENGTH_SHORT).show();

                }

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

     **/






    /**
     @Override
     public void onResume() {

     mCategoryRowAdapter.clear();

     mLoaderManager = LoaderManager.getInstance(this);
     mLoaderManager.initLoader(CATEGORY_LOADER, null, this);


     mCategoryRowAdapter = new ArrayObjectAdapter(new ListRowPresenter());
     setAdapter(mCategoryRowAdapter);
     super.onResume();
     }
     **/

    @Override
    public void onStop() {
        mBackgroundManager.release();
        super.onStop();
    }

    private void prepareBackgroundManager() {
        mBackgroundManager = BackgroundManager.getInstance(getActivity());
        mBackgroundManager.attach(getActivity().getWindow());
        mDefaultBackground = getResources().getDrawable(R.drawable.backgroundtv, null);
        mBackgroundTask = new UpdateBackgroundTask();
        mMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
    }

    private void setupUIElements() {
        setBadgeDrawable(
                getActivity().getResources().getDrawable(R.drawable.twende, null));
        setTitle(getString(R.string.browse_title)); // Badge, when set, takes precedent over title
        setHeadersState(HEADERS_ENABLED);
        setHeadersTransitionOnBackEnabled(true);

        // Set fastLane (or headers) background color
        setBrandColor(ContextCompat.getColor(getActivity(), R.color.fastlane_background));

        // Set search icon color.
        setSearchAffordanceColor(ContextCompat.getColor(getActivity(), R.color.search_opaque));

        setHeaderPresenterSelector(new PresenterSelector() {
            @Override
            public Presenter getPresenter(Object o) {
                return new IconHeaderItemPresenter();
            }
        });
    }

    private void setupEventListeners() {
        setOnSearchClickedListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //Intent intent = new Intent(getActivity(), SearchActivity.class);
                //startActivity(intent);
            }
        });

        setOnItemViewClickedListener(new ItemViewClickedListener());
        setOnItemViewSelectedListener(new ItemViewSelectedListener());
    }

    private void updateBackground(String uri) {
        int width = mMetrics.widthPixels;
        int height = mMetrics.heightPixels;

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .error(mDefaultBackground);

        Glide.with(getActivity())
                .asBitmap()
                .load(uri)
                .apply(options)
                .into(new SimpleTarget<Bitmap>(width, height) {
                    @Override
                    public void onResourceReady(
                            Bitmap resource,
                            Transition<? super Bitmap> transition) {
                        mBackgroundManager.setBitmap(resource);
                    }
                });
    }

    private void startBackgroundTimer() {
        mHandler.removeCallbacks(mBackgroundTask);
        mHandler.postDelayed(mBackgroundTask, BACKGROUND_UPDATE_DELAY);
    }

    private void updateRecommendations() {
        Intent recommendationIntent = new Intent(getActivity(), UpdateRecommendationsService.class);
        getActivity().startService(recommendationIntent);
    }



    private class UpdateBackgroundTask implements Runnable {

        @Override
        public void run() {
            if (mBackgroundURI != null) {
                updateBackground(mBackgroundURI.toString());
            }
        }
    }

    private final class ItemViewClickedListener implements OnItemViewClickedListener {
        @Override
        public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                                  RowPresenter.ViewHolder rowViewHolder, Row row) {

            if (item instanceof Video) {
                Video video = (Video) item;
                Intent intent = new Intent(getActivity(), VideoDetailsActivity.class);
                intent.putExtra(VideoDetailsActivity.VIDEO, video);

                Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
                        getActivity(),
                        ((ImageCardView) itemViewHolder.view).getMainImageView(),
                        VideoDetailsActivity.SHARED_ELEMENT_NAME).toBundle();
                getActivity().startActivity(intent, bundle);
            } else if (item instanceof String) {
                if (((String) item).contains(getString(R.string.guidedstep_first_title))) {
                    Intent intent = new Intent(getActivity(), GuidedStepActivity.class);
                    Bundle bundle =
                            ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
                                    .toBundle();
                    startActivity(intent, bundle);
                } else if (((String) item).contains(getString(R.string.error_fragment))) {
                    BrowseErrorFragment errorFragment = new BrowseErrorFragment();
                    getFragmentManager().beginTransaction().replace(R.id.movie_frame, errorFragment)
                            .addToBackStack(null).commit();
                } else if(((String) item).contains(getString(R.string.personal_settings))) {
                    Intent intent = new Intent(getActivity(), SettingsActivity.class);
                    Bundle bundle =
                            ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
                                    .toBundle();
                    startActivity(intent, bundle);
                } else {
                    Toast.makeText(getActivity(), ((String) item), Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }
    }


    private void loadRows() {
        /* GridItemPresenter */
        IconHeaderItem gridItemPresenterHeader = new IconHeaderItem(0, "More", R.drawable.tv_animation_d);

        GridItemPresenter mGridPresenter = new GridItemPresenter();
        ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
        /**
         gridRowAdapter.add(GRID_STRING_EXIT);
         gridRowAdapter.add(GRID_STRING_VIDEOS);

         gridRowAdapter.add(GRID_STRING_VERTICAL_GRID_FRAGMENT);
         gridRowAdapter.add(GRID_STRING_RECOMMENDATION);
         gridRowAdapter.add(GRID_STRING_SPINNER);
         **/
        mGridItemListRow = new CustomListRow(gridItemPresenterHeader, gridRowAdapter);
    }

    private void setRows() {
        mRowsAdapter = new CustumArrayObjectAdapter(new CustomListRowPresenter()); // Initialize

        if(mVideoListRowArray != null) {
            for (CustomListRow videoListRow : mVideoListRowArray) {
                mRowsAdapter.add(videoListRow);
            }
        }
        if(mGridItemListRow != null) {
            mRowsAdapter.add(mGridItemListRow);
        }

        /* Set */
        setAdapter(mRowsAdapter);

    }


    @Override
    public Loader<LinkedHashMap<String, List<Video>>> onCreateLoader(int id, Bundle args) {
        /* Create new Loader */
        Log.d(TAG, "onCreateLoader");
        if(id == VIDEO_ITEM_LOADER_ID) {
            Log.d(TAG, "create VideoItemLoader");
            //return new VideoItemLoader(getActivity());
            return new TvItemLoader(getActivity().getApplicationContext());
        }
        return null;
    }

    @Override
    public void onLoadFinished(Loader<LinkedHashMap<String, List<Video>>> loader, LinkedHashMap<String, List<Video>> data) {
        Log.d(TAG, "onLoadFinished");
        /* Loader data has prepared. Start updating UI here */
        switch (loader.getId()) {
            case VIDEO_ITEM_LOADER_ID:
                Log.d(TAG, "VideoLists UI update");

                /* Hold data reference to use it for recommendation */
                mItems = new ArrayList<Video>();

                /* loadRows: videoListRow - CardPresenter */
                int index = 1;
                mVideoListRowArray = new ArrayList<>();
                TvCardPresenter cardPresenter = new TvCardPresenter();

                if (null != data) {
                    for (Map.Entry<String, List<Video>> entry : data.entrySet()) {
                        ArrayObjectAdapter cardRowAdapter = new ArrayObjectAdapter(cardPresenter);
                        List<Video> list = entry.getValue();

                        for (int j = 0; j < list.size(); j++) {
                            Video movie = list.get(j);
                            cardRowAdapter.add(movie);
                            mItems.add(movie);           // Add movie reference for recommendation purpose.
                        }
                        IconHeaderItem header = new IconHeaderItem(index, entry.getKey(), R.drawable.tv_animation_d);
                        index++;
                        CustomListRow videoListRow = new CustomListRow(header, cardRowAdapter);
                        videoListRow.setNumRows(1);
                        mVideoListRowArray.add(videoListRow);
                    }
                } else {
                    Log.e(TAG, "An error occurred fetching videos");
                }

                /* Set */
                setRows();
        }
    }

    @Override
    public void onLoaderReset(Loader<LinkedHashMap<String, List<Video>>> loader) {
        Log.d(TAG, "onLoadReset");
        /* When it is called, Loader data is now unavailable due to some reason. */

    }



    private class GridItemPresenter extends Presenter {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent) {
            TextView view = new TextView(parent.getContext());
            view.setLayoutParams(new ViewGroup.LayoutParams(GRID_ITEM_WIDTH, GRID_ITEM_HEIGHT));
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            view.setBackgroundColor(getResources().getColor(R.color.default_background));
            view.setTextColor(Color.WHITE);
            view.setGravity(Gravity.CENTER);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, Object item) {
            ((TextView) viewHolder.view).setText((String) item);
        }

        @Override
        public void onUnbindViewHolder(ViewHolder viewHolder) {

        }
    }

    private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
        @Override
        public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                                   RowPresenter.ViewHolder rowViewHolder, Row row) {
            // each time the item is selected, code inside here will be executed.
            if (item instanceof String) {                    // GridItemPresenter
                startBackgroundTimer();
            } else if (item instanceof Video) {              // CardPresenter
                startBackgroundTimer();
            }
        }
    }


}


Java:
public class MainActivity extends LeanbackActivity {

    // Init
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            updateChannels();
            handler.postDelayed(this, 10000);
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        handler.postDelayed(runnable, 10000);

    }

    public void updateChannels(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiInterface.JSONURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        ApiInterface api = retrofit.create(ApiInterface.class);

        Call<String> call = api.getString();

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.i("Responsestring", response.body().toString());
                //Toast.makeText()
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        Log.i("onSuccess", response.body().toString());

                        String jsonresponse = response.body().toString();
                        chkResponse(jsonresponse);

                    } else {
                        Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }

    private void chkResponse(String response){

        try {
            //getting the whole json object from the response
            JSONObject obj = new JSONObject(response);
            if(obj.optString("update").equals("true"))
            {

                try {


                    Intent i = new Intent(MainActivity.this, MainFragment.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);

                } catch (Exception e)
                {
                    Toast.makeText(this, "Error Refreshing Channel Rows.", Toast.LENGTH_SHORT).show();

                }

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
 
Without running the code on the debugger I'd say your runnable is never being called. I'd wrap it in a function then call it from the main activity.

I don't think that's it. The onCreate calls it delayed with handler.postDelayed(runnable, 10000); and then it calls itself every 10 seconds after.

Hi,

Java:
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.i("Responsestring", response.body().toString());
                //Toast.makeText()
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        Log.i("onSuccess", response.body().toString());

                        String jsonresponse = response.body().toString();
                        chkResponse(jsonresponse);

                    } else {
                        Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

What are you seeing in your logs? If nothing, maybe add some logging to onFailure - you might be getting that triggered instead of onResponse and you've got nothing to tell you what's happening.
 
Upvote 0
I don't think that's it. The onCreate calls it delayed with handler.postDelayed(runnable, 10000); and then it calls itself every 10 seconds after.



What are you seeing in your logs? If nothing, maybe add some logging to onFailure - you might be getting that triggered instead of onResponse and you've got nothing to tell you what's happening.
Hi, I am sure the handler is running (tested handler with toast in both the main fragment and activity) plus the blinking effect proves that there was a refresh but the listrow is not being updated.
 
Upvote 0
Without running the code on the debugger I'd say your runnable is never being called. I'd wrap it in a function then call it from the main activity.
Hi, I am sure the handler is running (tested handler with toast in both the main fragment and activity) plus the blinking effect proves that there was a refresh but the listrow is not being updated.
 
Upvote 0
Here is a piece of my global class where you can set and get stuff and pass data and objects between activities and even inside closures.

Code:
public static ListView listView=new ListView;
/* end variable definitions */



    /*start methods */





//get and set global list view.
    public void setListView(ListView list0){

        listView=list0;
    }

    public ListView getListView(){

        return listView;
    }

    public void reSetLock2Csplit00(){

        listView.clear();
    }
 
Upvote 0
The timer is a closure then. Do you have a global class where you can set and get the listview.?

hi yes I do. There are two other class files that fetch the json list and load the json data

Java:
public class VideoProvider  {

    private static final String TAG = "VideoProvider";

    public  static final String TV_LIST_URL = "http://192.168.0.34/test/getTvChannels.php";

    private static String TAG_ID = "id";
    private static String TAG_MEDIA = "videos";
    private static String TAG_VIDEO_LISTS = "LiveTV";
    private static String TAG_CATEGORY = "category";
    private static String TAG_STUDIO = "studio";
    private static String TAG_SOURCES = "source";
    private static String TAG_DESCRIPTION = "description";
    private static String TAG_CARD_THUMB = "card";
    private static String TAG_BACKGROUND = "background";
    private static String TAG_TITLE = "title";

    private static LinkedHashMap<String, List<Video>> sTvList;




    private static Resources sResources;
    Context mcontext;

    public static void setContext(Context context) {
        if (null == sResources) {
            sResources = context.getResources();
        }

    }



    public static LinkedHashMap<String, List<Video>> getMedia() {
        return sTvList;
    }

    public static ArrayList<Video> getMovieItems(String category) {
        if(sTvList == null) {
            Log.e(TAG, "sMovieList is not prepared yet!");
            return null;
        } else {
            ArrayList<Video> movieItems = new ArrayList<>();
            for (Map.Entry<String, List<Video>> entry : sTvList.entrySet()) {
                String categoryName = entry.getKey();
                if(category !=null && !category.equals(categoryName)) {
                    continue;
                }
                List<Video> list = entry.getValue();
                for (int j = 0; j < list.size(); j++) {
                    movieItems.add(list.get(j));
                }
            }
            if(movieItems == null) {
                Log.w(TAG, "No data foud with category: " + category);
            }
            return movieItems;
        }
    }

    public static LinkedHashMap<String, List<Video>> buildMedia(Context ctx) throws JSONException {
        return buildMedia(ctx, TV_LIST_URL);
    }

    public static LinkedHashMap<String, List<Video>> buildMedia(Context ctx, String url)
            throws JSONException {
        if (null != sTvList) {
            return sTvList;
        }
        sTvList = new LinkedHashMap<>();
        //sMovieListById = new HashMap<>();

        //JSONObject jsonObj = parseUrl(VIDEO_LIST_URL);

        //JSONObject jsonObj = new getJSON(VIDEO_LIST_URL);

        JsonParser jParser = new JsonParser();
        JSONObject jsonObj = jParser.getJSONFromUrl(TV_LIST_URL);

        if (null == jsonObj) {
            Log.e(TAG, "An error occurred fetching video");
            return sTvList;
        }

        JSONArray categories = jsonObj.getJSONArray(TAG_VIDEO_LISTS);

        if (null != categories) {

            final int categoryLength = categories.length();
            Log.d(TAG, "category #: " + categoryLength);
            long id;
            String title;
            String videoUrl;
            String bgImageUrl;
            String cardImageUrl;
            String studio;
            for (int catIdx = 0; catIdx < categoryLength; catIdx++) {
                JSONObject category = categories.getJSONObject(catIdx);
                String categoryName = category.getString(TAG_CATEGORY);
                JSONArray videos = category.getJSONArray(TAG_MEDIA);
                Log.d(TAG,
                        "category: " + catIdx + " Name:" + categoryName + " video length: "
                                + (null != videos ? videos.length() : 0));
                List<Video> categoryList = new ArrayList<Video>();
                Video movie;
                if (null != videos) {
                    for (int vidIdx = 0, vidSize = videos.length(); vidIdx < vidSize; vidIdx++) {
                        JSONObject video = videos.getJSONObject(vidIdx);
                        String description = video.getString(TAG_DESCRIPTION);
                        JSONArray videoUrls = video.getJSONArray(TAG_SOURCES);
                        if (null == videoUrls || videoUrls.length() == 0) {
                            continue;
                        }
                        id = vidIdx + 1;
                        title = video.getString(TAG_TITLE);
                        videoUrl = getVideoSourceUrl(videoUrls);
                        bgImageUrl = video.getString(TAG_BACKGROUND);
                        cardImageUrl = video.getString(TAG_CARD_THUMB);
                        studio = video.getString(TAG_STUDIO);

                        Log.d(TAG, "card #: " + cardImageUrl);

                        movie = buildMovieInfo(id, categoryName, title, description, studio,
                                videoUrl, cardImageUrl, bgImageUrl);
                        categoryList.add(movie);
                    }
                    sTvList.put(categoryName, categoryList);
                }
            }
        }
        return sTvList;
    }

    private static Video buildMovieInfo(long id,
                                        String category,
                                        String title,
                                        String description,
                                        String studio,
                                        String videoUrl,
                                        String cardImageUrl,
                                        String bgImageUrl) {
        Video channel = new Video();
        channel.setId(id);
        channel.setTitle(title);
        channel.setDescription(description);
        channel.setStudio(studio);
        channel.setCategory(category);
        channel.setCardImageUrl(cardImageUrl);
        channel.setBackgroundImageUrl(bgImageUrl);
        channel.setVideoUrl(videoUrl);

        return channel;
    }

    // workaround for partially pre-encoded sample data
    private static String getVideoSourceUrl(final JSONArray videos) throws JSONException {
        try {
            final String url = videos.getString(0);
            return (-1) == url.indexOf('%') ? url : URLDecoder.decode(url, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new JSONException("Broken VM: no UTF-8");
        }
    }

}


Java:
public class TvItemLoader extends AsyncTaskLoader<LinkedHashMap<String, List<Video>>> {

    private static final String TAG = TvItemLoader.class.getSimpleName();
    LinkedHashMap<String, List<Video>> mData;

    public TvItemLoader(Context context) {
        super(context);
    }

    @Override
    public LinkedHashMap<String, List<Video>> loadInBackground() {
        Log.d(TAG, "loadInBackground");

        /*
         * Executed in background thread.
         * Prepare data here, it may take long time (Database access, URL connection, etc).
         * return value is used in onLoadFinished() method in Activity/Fragment's LoaderCallbacks.
         */
        //LinkedHashMap<String, List<Movie>> videoLists = prepareData();
        LinkedHashMap<String, List<Video>> videoLists = null;
        try {
            videoLists = VideoProvider.buildMedia(getContext());
        } catch (JSONException e) {
            Log.e(TAG, "buildMedia failed", e);
            //cancelLoad();
        }
        return videoLists;
    }

    @Override
    public void deliverResult(LinkedHashMap<String, List<Video>> data) {
        Log.d(TAG, "deliverResult");

        LinkedHashMap<String, List<Video>> oldData = mData;
        mData = data;

        if(isStarted()){
            Log.d(TAG, "isStarted true");
            super.deliverResult(data);
        }

        if(oldData != null && oldData != data) {
            releaseResources(oldData);
        }
    }

    private void releaseResources(LinkedHashMap<String, List<Video>> data) {
        Log.d(TAG, "releaseResources");
        // For a simple List, there is nothing to do. For something like a Cursor, we
        // would close it in this method. All resources associated with the Loader
        // should be released here.
        data = null;
    }

    @Override
    protected void onStartLoading() {
        Log.d(TAG, "onStartLoading");
        if (mData != null) {
            Log.d(TAG, "mData remaining");
            deliverResult(mData);
        } else {
            Log.d(TAG, "mData is null, forceLoad");
            forceLoad();
        }
        //super.onStartLoading();

    }

    @Override
    protected void onStopLoading() {
        Log.d(TAG, "onStopLoading");
        //super.onStopLoading();
        cancelLoad();
    }

    @Override
    protected void onReset() {
        Log.d(TAG, "onReset");
        super.onReset();
    }

    @Override
    public void onCanceled(LinkedHashMap<String, List<Video>> data) {
        Log.d(TAG, "onCanceled");
        super.onCanceled(data);
    }

    @Override
    protected boolean onCancelLoad() {
        return super.onCancelLoad();
    }

}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones