Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use getSupportLoaderManager() in Fragments?

I want to load information from an API to a Fragment.

I use getSupportLoaderManager() but an error is occurring:

Cannot resolve method: getSupportLoaderManager()

I search for this use onActivityCreated() but I have NullPointerException.

Can you help me to solve the issue?

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class Business extends Fragment implements LoaderManager.LoaderCallbacks<String> {

    ListView listView;   

    public Business() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_business, container, false);
        listView = view.findViewById(R.id.list_item);

        getSupportLoaderManager().initLoader(0, null, getContext()).forceLoad();
        return view;
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return null;
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null && !data.isEmpty())
            updateUI(data);
        else
            Toast.makeText(getContext(), "no internet", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {

    }

    private void updateUI(String data) {
        try {
            final ArrayList<ResultOfArtBusiness> results = new ArrayList<>();

            JSONObject root = new JSONObject(data);
            JSONObject secondObject = root.getJSONObject("response");

            JSONArray firstArray = secondObject.getJSONArray("results");
            for (int i = 0; i < firstArray.length(); i++) {
                JSONObject objectInArray = firstArray.getJSONObject(i);
                String webPublicationDate;
                if (objectInArray.has("webPublicationDate"))
                    webPublicationDate =   objectInArray.getString("webPublicationDate");
                else webPublicationDate = null;

                String webTitle;
                if (objectInArray.has("webTitle"))
                    webTitle = objectInArray.getString("webTitle");
                else webTitle = null;

                String webUrl;
                if (objectInArray.has("webUrl"))
                    webUrl = objectInArray.getString("webUrl");
                else webUrl = null;

                ResultOfArtBusiness resultOfArticle = new ResultOfArtBusiness(webTitle, webPublicationDate, webUrl);
                results.add(resultOfArticle);
            }

            NewsAdapterBusiness adapter = new NewsAdapterBusiness(getContext(), R.layout.item_design, results);

            listView.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
like image 510
ShahidaTalib Avatar asked Dec 21 '17 18:12

ShahidaTalib


Video Answer


1 Answers

Update: Sept 2, 2019

getLoaderManager() in Fragment is deprecated.

use

// LoaderManager#getInstance(LifecycleOwner) 
LoaderManager.getInstance(this) // this points to the Fragment

Old Answer

The thing is that we use getLoaderManager() or getSupportLoaderManager() when calling from activity but when it comes to fragment you can directly call getLoaderManager() no need to call getSupportLoaderManager() and behind the scenes it will call its respective versions of the method depending upon the fragment used.

like image 145
vikas kumar Avatar answered Nov 03 '22 17:11

vikas kumar