it's the first time I'm using Loader. After calling the method
getLoaderManager().initLoader(0, null, this);
I'm getting an error in the argument "this".
Error after compiling:
error: method initLoader in class LoaderManager cannot be applied to given types; getLoaderManager().initLoader(0, null, this); ^ required: int,Bundle,LoaderCallbacks found: int,,MainActivity reason: cannot infer type-variable(s) D (argument mismatch; MainActivity cannot be converted to LoaderCallbacks) where D is a type-variable: D extends Object declared in method initLoader(int,Bundle,LoaderCallbacks)
My code:
package com.example.newsapp;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<News>> {
private static final String URL = "http://content.guardianapis.com/search";
private NewsAdapter newsAdapter;
private TextView emptyListStateTextView;
private Intent intent;
ListView newsListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("MainActivity", "RUN onCreate...");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newsListView = findViewById(R.id.listView);
emptyListStateTextView = findViewById(R.id.defautView);
newsListView.setEmptyView(emptyListStateTextView);
newsAdapter = new NewsAdapter(this, new ArrayList<News>());
newsListView.setAdapter(newsAdapter);
newsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News currentNews = newsAdapter.getItem(position);
Uri uri = Uri.parse(currentNews.getUrl());
intent= new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
View loadingIndicator = findViewById(R.id.loading_bar);
loadingIndicator.setVisibility(View.GONE);
emptyListStateTextView.setText("No internet connection");
}else{
getLoaderManager().initLoader(0, null, this);
}
Log.e("MainActivity", "DONE onCreate...");
}
@NonNull
@Override
public Loader<List<News>> onCreateLoader(int i, Bundle args) {
Log.e("MainActivity", "RUN onCreateLoader...");
//TEST
Uri baseUri = Uri.parse(URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("show-tags", "contributor");
uriBuilder.appendQueryParameter("section", "technology");
uriBuilder.appendQueryParameter("order-by", "newest");
uriBuilder.appendQueryParameter("from-date", "2018-01-01");
uriBuilder.appendQueryParameter("api-key", "test");
Log.v("my_tag", "url created is: "+uriBuilder.toString());
//TEST
Log.e("MainActivity", "DONE onCreateLoader...");
return new NewsLoader(this, uriBuilder.toString());
}
@Override
public void onLoadFinished(@NonNull Loader<List<News>> loader, List<News> data) {
Log.e("MainActivity", "RUN onLoadFinished...");
View loadingBar = findViewById(R.id.loading_bar);
loadingBar.setVisibility(View.GONE);
newsAdapter.clear();
if(data != null && !data.isEmpty()){
newsAdapter.addAll(data);
}
Log.e("MainActivity", "DONE onLoadFinished...");
}
@Override
public void onLoaderReset(@NonNull Loader<List<News>> loader) {
Log.e("MainActivity", "RUN onLoaderReset...");
newsAdapter.clear();
Log.e("MainActivity", "DONE onLoaderReset...");
}
}
I personally believe, that the problem must be somewhere at the imports. Even though I will really appreciate any kind of help.
Thank you!
getLoaderManager
has been depreciated, use getSupportLoaderManager
instead like this:
getSupportLoaderManager().initLoader(0, null, this);
However if you still want to use this, it is due to an incorrect import. Import loader call back and it should fix the issue
android.app.LoaderCallbacks