下面是earthquakeactivity.java
package android.example.quakereports;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class EarthquakeActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Earthquake>> {
private quakeAdapter mAdapter;
/**
* URL for earthquake data from the USGS dataset
*/
private static final String USGS_REQUEST_URL =
"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=5&limit=10";
private static final int EARTHQUAKE_LOADER_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView earthquakeListView = (ListView) findViewById(R.id.list);
mAdapter = new quakeAdapter(this, new ArrayList<Earthquake>());
earthquakeListView.setAdapter(mAdapter);
LoaderManager loaderManager=getLoaderManager();// gives error here
loaderManager.initLoader(EARTHQUAKE_LOADER_ID,null,this);
earthquakeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Earthquake currentquake = (Earthquake) mAdapter.getItem(position);
Uri earthQuakeUri = Uri.parse(currentquake.getUrl());
Intent i = new Intent(Intent.ACTION_VIEW, earthQuakeUri);
startActivity(i);
}
});
}
@NonNull
@Override
public Loader<List<Earthquake>> onCreateLoader(int id, Bundle args) {
return new myAsyncTaskLoader(this,USGS_REQUEST_URL);
}
@Override
public void onLoadFinished(Loader<List<Earthquake>> loader, List<Earthquake> data) {
// Clear the adapter of previous earthquake data
mAdapter.clear();
// If there is a valid list of {@link Earthquake}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
@Override
public void onLoaderReset(Loader<List<Earthquake>> loader) {
mAdapter.clear();
}
}
这里是masynctaskloader.java
package android.example.quakereports;
import android.content.Context;
import androidx.loader.content.AsyncTaskLoader;
import java.util.List;
public class myAsyncTaskLoader extends AsyncTaskLoader<List<Earthquake>> {
private String mUrl=null;
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public List<Earthquake> loadInBackground() {
if(mUrl==null)
{
return null;
}
return QueryUtils.fetchEarthquakeData(mUrl);
}
public myAsyncTaskLoader(Context context, String url)
{
super(context);
mUrl=url;
}
}
myasynctaskloader.java文件没有问题,它工作正常。在asynctaskloader和loadermanager涉及的任何地方,我都使用过androidx库。我已经在我面临错误的那一行上发表了评论。有没有什么方法可以在没有任何错误的情况下初始化我的加载程序,或者我应该使用 import android.app.LoaderManager.LoaderCallbacks;
像android支持库一样,这是唯一的选择吗?
暂无答案!
目前还没有任何答案,快来回答吧!