简体   繁体   中英

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();
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM