Я пытаюсь реализовать простой Loader, и это мой код
public class EarthquakeActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Quake>> {
/**
* Constant value for the earthquake loader ID. We can choose any integer.
* This really only comes into play if you're using multiple loaders.
*/
private static final int EARTHQUAKE_LOADER_ID = 1;
/** 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&eventtype=earthquake&orderby=time&minmag=6&limit=10";
public static final String TAG = EarthquakeActivity.class.getName();
//Made it global as it needs to be accessed by other class too
private QuakeAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.e(TAG,"OnCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.earthquake_activity);
// Find a reference to the {@link ListView} in the layout
ListView earthquakeListView = (ListView) findViewById(R.id.list);
// Create a new adapter that takes an empty list of earthquakes as input
mAdapter = new QuakeAdapter(this, new ArrayList<Quake>());
// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
earthquakeListView.setAdapter(mAdapter);
// Get a reference to the LoaderManager, in order to interact with loaders.
LoaderManager loaderManager = getSupportLoaderManager();
// Initialize the loader. Pass in the int ID constant defined above and pass in null for
// the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
// because this activity implements the LoaderCallbacks interface).
loaderManager.initLoader(EARTHQUAKE_LOADER_ID, null, this);
Log.e(TAG,"loaderManager.initLoader()");
// Set an item click listener on the ListView, which sends an intent to a web browser
// to open a website with more information about the selected earthquake.
earthquakeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Find the current earthquake that was clicked on
Quake currentEarthquake = mAdapter.getItem(position);
// Convert the String URL into a URI object (to pass into the Intent constructor)
Uri earthquakeUri = Uri.parse(currentEarthquake.getLink());
// Create a new intent to view the earthquake URI
Intent websiteIntent = new Intent(Intent.ACTION_VIEW, earthquakeUri);
// Send the intent to launch a new activity
startActivity(websiteIntent);
}
});
}
@NonNull
@Override
public Loader<List<Quake>> onCreateLoader(int i, Bundle bundle) {
// Create a new loader for the given URL
Log.e(TAG,"OnCreateLoader");
return new EarthquakeLoader(this, USGS_REQUEST_URL);
}
@Override
public void onLoadFinished(Loader<List<Quake>> loader, List<Quake> earthquakes) {
// 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 (earthquakes != null && !earthquakes.isEmpty()) {
mAdapter.addAll(earthquakes);
mAdapter.notifyDataSetChanged();
Log.e(TAG,"OnLoadFinished");
}
}
@Override
public void onLoaderReset(@NonNull Loader<List<Quake>> loader) {
// Loader reset, so we can clear out our existing data.
mAdapter.clear();
Log.e(TAG,"OnLoaderReset");
}
}
Все это прекрасно работает, но сообщения журнала, особенно их порядок, меня смущает. Вот они:
2020-04-02 17:48:26.989 3716-3716/com.example.android.quakereport E/com.example.android.quakereport.EarthquakeActivity: OnCreate()
2020-04-02 17:48:27.249 3716-3716/com.example.android.quakereport E/com.example.android.quakereport.EarthquakeActivity: OnCreateLoader
2020-04-02 17:48:27.255 3716-3716/com.example.android.quakereport E/com.example.android.quakereport.EarthquakeActivity: loaderManager.initLoader()
2020-04-02 17:48:27.272 3716-3716/com.example.android.quakereport E/com.example.android.quakereport.EarthquakeLoader: OnStartLoading()
2020-04-02 17:48:27.276 3716-3738/com.example.android.quakereport E/com.example.android.quakereport.EarthquakeLoader: loadInBackground
2020-04-02 17:48:29.698 3716-3738/com.example.android.quakereport E/com.example.android.quakereport.EarthquakeLoader: loadInBackground:fetchEarthquakeData()
2020-04-02 17:48:29.702 3716-3716/com.example.android.quakereport E/com.example.android.quakereport.EarthquakeActivity: OnLoadFinished
Разве метод initLoader () менеджера загрузчиков не должен проверять существующий загрузчик перед вызовом метода onCreateLoader ()? Отчеты журнала, казалось, предполагали, что это происходит в другом порядке, и я не понимаю, почему это происходит. Буду очень признателен за любые идеи, так как я начинающий