Hi I am using AsyncTaskLoader in my app and I have implemented that in MovieTaskLoader class but when I am implementing Loader callbacks in my fragment I am getting type conversion error in onCreateLoader() method. MovieTaskLoader class:
class MovieTaskLoader extends AsyncTaskLoader<ArrayList<Movies>> {
MovieTaskLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public ArrayList<Movies> loadInBackground() {
//Building URL
URL url = null;
String jsonData = null;
Uri uri = Uri.parse(Utility.BASE_URL).buildUpon()
.appendPath("popular")
.appendQueryParameter("api_key", BuildConfig.api_key)
.build();
try {
url = new URL(uri.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
//Downloading json data
HttpURLConnection urlConnection;
try {
assert url != null;
urlConnection = (HttpURLConnection) url.openConnection();
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
jsonData = readStream(urlConnection.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
}
return getMovieData(jsonData);
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder response = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}
private ArrayList<Movies> getMovieData(String jsonData){
ArrayList<Movies> movies = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray results = jsonObject.getJSONArray("results");
for(int i=0; i<20; i++){
Movies movie = new Movies();
JSONObject object = results.getJSONObject(i);
movie.setTitle(object.getString("title"));
movie.setSynopsis("overview");
movie.setVote_avg("vote_average");
movie.setDate("release_date");
movies.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
return movies;
}
}
Fragment class:
public class PopularFragment extends Fragment implements LoaderManager.LoaderCallbacks<ArrayList<Movies>> {
public PopularFragment() {
// 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_popular, container, false);
RecyclerView popularView = (RecyclerView) view.findViewById(R.id.popular_view);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
popularView.setLayoutManager(layoutManager);
popularView.setAdapter(new RecyclerViewAdapter());
return view;
}
@Override
public Loader<ArrayList<Movies>> onCreateLoader(int id, Bundle args) {
return new MovieTaskLoader(getContext());
}
@Override
public void onLoadFinished(Loader<ArrayList<Movies>> loader, ArrayList<Movies> data) {
}
@Override
public void onLoaderReset(Loader<ArrayList<Movies>> loader) {
}
}
I am getting error in onCreateLoader() method and type conversion error is:
Required: "Loader"
Found: MovieTaskLoader
Check your imports. Make sure both are same versions like this
android.support.v4.content.AsyncTaskLoader<D>
android.support.v4.app.LoaderManager.LoaderCallbacks<D>