Skip to content

Commit

Permalink
验证 Loader 行为
Browse files Browse the repository at this point in the history
  • Loading branch information
heyblood committed Jan 26, 2021
1 parent 732b6ca commit ca58a71
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
13 changes: 10 additions & 3 deletions app/src/main/java/com/example/quakereport/EarthquakeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -58,6 +56,8 @@ public class EarthquakeActivity extends AppCompatActivity implements LoaderManag

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "TEST: onCreate() called ...");

super.onCreate(savedInstanceState);
setContentView(R.layout.earthquake_activity);

Expand Down Expand Up @@ -96,18 +96,23 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
// 初始化 loader。传递上面定义的整数 ID 常量并作为捆绑
// 传递 null。为 LoaderCallbacks 参数(由于
// 此活动实现了 LoaderCallbacks 接口而有效)传递此活动。
Log.i(LOG_TAG, "TEST: loaderManager.initLoader() called ...");
loaderManager.initLoader(EARTHQUAKE_LOADER_ID, null, this);
}

@NonNull
@Override
public Loader<List<Earthquake>> onCreateLoader(int id, @Nullable Bundle args) {
Log.i(LOG_TAG, "TEST: onCreateLoader() called ...");

// 为给定 URL 创建新 loader
return new EarthquakeLoader(this, USGS_REQUEST_URL);
}

@Override
public void onLoadFinished(@NonNull Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
Log.i(LOG_TAG, "TEST: onLoadFinished() called ...");

// 清除之前地震数据的适配器
earthquakeAdapter.clear();

Expand All @@ -120,8 +125,10 @@ public void onLoadFinished(@NonNull Loader<List<Earthquake>> loader, List<Earthq

@Override
public void onLoaderReset(@NonNull Loader<List<Earthquake>> loader) {
Log.i(LOG_TAG, "TEST: onLoaderReset() called ...");

// 重置 Loader,以便能够清除现有数据。
earthquakeAdapter.clear();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.quakereport;

import android.content.Context;
import android.util.Log;

import androidx.loader.content.AsyncTaskLoader;

Expand Down Expand Up @@ -35,6 +36,9 @@ public EarthquakeLoader(Context context, String url) {

@Override
protected void onStartLoading() {
Log.i(LOG_TAG, "TEST: onStartLoading() called ...");

// Loader 初始化完成后,立即强制启动
forceLoad();
}

Expand All @@ -43,6 +47,8 @@ protected void onStartLoading() {
*/
@Override
public List<Earthquake> loadInBackground() {
Log.i(LOG_TAG, "TEST: loadInBackground() called ...");

if (null == url) {
return null;
}
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/com/example/quakereport/QueryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
Expand Down Expand Up @@ -184,6 +181,8 @@ private static List<Earthquake> extractFeatureFromJson(String earthquakeJSON) {
* 查询 USGS 数据集并返回 {@link Earthquake} 对象的列表。
*/
public static List<Earthquake> fetchEarthquakeData(String requestUrl) {
Log.i(LOG_TAG, "TEST: fetchEarthquakeData() called ...");

// 创建 URL 对象
URL url = createUrl(requestUrl);

Expand Down

0 comments on commit ca58a71

Please sign in to comment.