如何在Fragment中实现Loaders?

问题描述 投票:0回答:1

如何在片段中实现加载器。我在onCreateLoader()中收到一个错误,当我返回它表示不兼容类型的值时:必需:android.support.v4.content.Loader。如果你在FragmentChanging.class中向下滚动,你会发现OnCreateLoader()并且我评论了“不兼容的类型”。所以你可以轻松识别。我该如何解决这个问题。先感谢您。

fragment changing.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;
        }
    }
}
java android android-fragments android-loadermanager android-loader
1个回答
1
投票

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

import android.support.v4.content.AsyncTaskLoader;
© www.soinside.com 2019 - 2024. All rights reserved.