如何在 Fragment 中实现 Loader?

how to implement Loaders in Fragment?

如何在片段中实现加载器。我在 onCreateLoader() 中遇到错误, 当我 return 它说不兼容类型的值时:需要:android.support.v4.content.Loader。 如果您在 FragmentChanging.class 中向下滚动,您将找到 OnCreateLoader() 并且我已注释 "Incompatible types." 以便您轻松识别。 我该如何解决这个问题。提前谢谢你。

FragmentChanging.java

package com.howaboutthis.satyaraj.wallpaper;



import android.support.v4.app.Fragment;
import android.support.v4.content.Loader;
import android.content.Context;
import android.content.DialogInterface;    
import android.support.v4.app.LoaderManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import java.util.List;
import java.util.Objects;


public class FragmentChanging extends Fragment implements LoaderManager.LoaderCallbacks  {

    private ProgressDialog dialog;

    public FragmentChanging(){
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.fragment_changing_wallpaper, container, false);

        return view;
    }

    @Override
    public Loader onCreateLoader(int id, Bundle args) {
        if (id == 0 || id == 2){
            if (id==0)
                dialog.setMessage("Checking Connectivity...");
            if (id == 2)
                dialog.setMessage("Loading Settings...");
            dialog.show();
            return new TestInternetLoader(getContext()); //Incompatible types.            }

        return null;
    }


    @Override
    public void onLoadFinished(Loader loader, Object data) {
        int id = loader.getId();

        if (id == 0 || id == 2){
            boolean check = (Boolean) data;
            if (check) {
                if (dialog.isShowing()) {dialog.dismiss();}

            }
            else{

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());

                alertDialog.setTitle("Info");
                alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");
                alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                alertDialog.setNeutralButton("OK",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface arg0,
                                                int arg1) {

                            }
                        });

                alertDialog.show();

            }
        }

    }

    @Override
    public void onLoaderReset(Loader loader) {

    }

}

TestInternetLoader

package com.howaboutthis.satyaraj.changing;

import android.content.AsyncTaskLoader;
import android.content.Context;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class TestInternetLoader extends AsyncTaskLoader {

    TestInternetLoader(Context context) {
        super(context);
    }

    @Override
    protected void onStartLoading(){
        forceLoad();
    }

    @Override
    public Object loadInBackground() {
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection)(new URL("http://www.google.com").openConnection());
            httpURLConnection.setRequestProperty("User-Agent", "Test");
            httpURLConnection.setRequestProperty("Connection", "close");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.connect();
            return (httpURLConnection.getResponseCode() == 200);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            return false;
        }
        catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

您的 TestInternetLoader 导入错误。 import android.content.AsyncTaskLoader;应该是

import android.support.v4.content.AsyncTaskLoader;