gpt4 book ai didi

java - 如何在Fragment中实现Loader?

转载 作者:行者123 更新时间:2023-12-02 11:47:32 27 4
gpt4 key购买 nike

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

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) {

}

}

测试互联网加载器

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 中的导入错误。 导入 android.content.AsyncTaskLoader; 应该是

import android.support.v4.content.AsyncTaskLoader;

关于java - 如何在Fragment中实现Loader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100785/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com