Skip to content

Commit

Permalink
练习:显示有关每次地震的详细信息
Browse files Browse the repository at this point in the history
  • Loading branch information
heyblood committed Jan 20, 2021
1 parent edb7aba commit be93e41
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 10 deletions.
25 changes: 25 additions & 0 deletions app/src/main/java/com/example/quakereport/Earthquake.java
@@ -0,0 +1,25 @@
package com.example.quakereport;

public class Earthquake {
private String mag;
private String place;
private String time;

public String getMag() {
return mag;
}

public String getPlace() {
return place;
}

public String getTime() {
return time;
}

public Earthquake(String mag, String place, String time) {
this.mag = mag;
this.place = place;
this.time = time;
}
}
23 changes: 13 additions & 10 deletions app/src/main/java/com/example/quakereport/EarthquakeActivity.java
Expand Up @@ -33,21 +33,24 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.earthquake_activity);

// Create a fake list of earthquake locations.
ArrayList<String> earthquakes = new ArrayList<>();
earthquakes.add("San Francisco");
earthquakes.add("London");
earthquakes.add("Tokyo");
earthquakes.add("Mexico City");
earthquakes.add("Moscow");
earthquakes.add("Rio de Janeiro");
earthquakes.add("Paris");
ArrayList<Earthquake> earthquakes = new ArrayList<>();
earthquakes.add(new Earthquake("4.6", "Fiji region", "Jan 19, 2021"));
earthquakes.add(new Earthquake("4.5", "70 km NNE of Hihifo, Tonga", "Jan 19, 2021"));
earthquakes.add(new Earthquake("4.7", "74 km SE of Lebedinyy, Russia", "Jan 18, 2021"));
earthquakes.add(new Earthquake("5.2", "South Sandwich Islands region", "Jan 17, 2021"));
earthquakes.add(new Earthquake("4.6", "Fiji region", "Jan 19, 2021"));
earthquakes.add(new Earthquake("4.5", "70 km NNE of Hihifo, Tonga", "Jan 19, 2021"));
earthquakes.add(new Earthquake("4.7", "74 km SE of Lebedinyy, Russia", "Jan 18, 2021"));
earthquakes.add(new Earthquake("5.2", "South Sandwich Islands region", "Jan 17, 2021"));
earthquakes.add(new Earthquake("4.6", "Fiji region", "Jan 19, 2021"));
earthquakes.add(new Earthquake("4.5", "70 km NNE of Hihifo, Tonga", "Jan 19, 2021"));
earthquakes.add(new Earthquake("4.7", "74 km SE of Lebedinyy, Russia", "Jan 18, 2021"));

// Find a reference to the {@link ListView} in the layout
ListView earthquakeListView = (ListView) findViewById(R.id.list);

// Create a new {@link ArrayAdapter} of earthquakes
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, earthquakes);
EarthquakeAdapter adapter = new EarthquakeAdapter(this, earthquakes);

// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
Expand Down
53 changes: 53 additions & 0 deletions app/src/main/java/com/example/quakereport/EarthquakeAdapter.java
@@ -0,0 +1,53 @@
package com.example.quakereport;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class EarthquakeAdapter extends ArrayAdapter<Earthquake> {

public EarthquakeAdapter(@NonNull Context context, @NonNull List<Earthquake> objects) {
super(context, 0, objects);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View itemView = convertView;
Earthquake earthquake;
ViewHolder holder;

if (null == itemView) {
itemView = LayoutInflater.from(getContext()).inflate(R.layout.earthquake_item, null);
holder = new ViewHolder();
holder.tvMag = itemView.findViewById(R.id.mag_text);
holder.tvPlace = itemView.findViewById(R.id.place_text);
holder.tvTime = itemView.findViewById(R.id.time_text);
itemView.setTag(holder);
} else {
holder = (ViewHolder) itemView.getTag();
}

earthquake = getItem(position);

holder.tvMag.setText(earthquake.getMag());
holder.tvPlace.setText(earthquake.getPlace());
holder.tvTime.setText(earthquake.getTime());

return itemView;
}

private static class ViewHolder {
private TextView tvMag;
private TextView tvPlace;
private TextView tvTime;
}
}
31 changes: 31 additions & 0 deletions app/src/main/res/layout/earthquake_item.xml
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/dp_16">

<TextView
android:id="@+id/mag_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="8.0" />

<TextView
android:id="@+id/place_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
tools:text="Wenchuan Sichuan" />

<TextView
android:id="@+id/time_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="right"
tools:text="May 12, 2008" />

</LinearLayout>

0 comments on commit be93e41

Please sign in to comment.