Skip to content

Commit

Permalink
处理列表的空状态
Browse files Browse the repository at this point in the history
  • Loading branch information
heyblood committed Jan 26, 2021
1 parent ca58a71 commit 6a693e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -48,12 +49,18 @@ public class EarthquakeActivity extends AppCompatActivity implements LoaderManag
* 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=20";
"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=5&limit=0";

/**
* 地震列表的适配器
*/
private EarthquakeAdapter earthquakeAdapter;

/**
* 列表为空时显示的 空视图
*/
private TextView emptyView;

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "TEST: onCreate() called ...");
Expand All @@ -64,6 +71,10 @@ protected void onCreate(Bundle savedInstanceState) {
// 在布局中查找 {@link ListView} 的引用
ListView earthquakeListView = (ListView) findViewById(R.id.list);

// 为 ListView 绑定空视图,在无数据时显示
emptyView = (TextView) findViewById(R.id.empty_view);
earthquakeListView.setEmptyView(emptyView);

// 创建新适配器,将空地震列表作为输入
earthquakeAdapter = new EarthquakeAdapter(this, new ArrayList<Earthquake>());

Expand Down Expand Up @@ -113,6 +124,9 @@ public Loader<List<Earthquake>> onCreateLoader(int id, @Nullable Bundle args) {
public void onLoadFinished(@NonNull Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
Log.i(LOG_TAG, "TEST: onLoadFinished() called ...");

// Set empty state text to display "No earthquakes found."
emptyView.setText(R.string.no_earthquakes);

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

Expand Down
40 changes: 19 additions & 21 deletions app/src/main/res/layout/earthquake_activity.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

http://www.apache.org/licenses/LICENSE-2.0
<!-- Layout for a list of earthquakes -->
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:orientation="vertical" />

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Layout for a list of earthquakes -->
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"/>
<!-- Empty view is only visible when the list has no items. -->
<TextView
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="?android:textAppearanceMedium" />
</RelativeLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<resources>
<string name="app_name">Quake Report</string>

<!-- Default text to show with the earthquake location ("Near the Pacific-Antarctic Ridge")
if no specific kilometer distance from the primary location is given [CHAR LIMIT=30] -->
<string name="near_the">Near the</string>

<!-- Text to display in the list when there are no earthquakes [CHAR LIMIT=NONE] -->
<string name="no_earthquakes">No earthquakes found.</string>

</resources>

0 comments on commit 6a693e5

Please sign in to comment.