Skip to content

Commit

Permalink
添加地震 Intent
Browse files Browse the repository at this point in the history
  • Loading branch information
heyblood committed Jan 21, 2021
1 parent 5574906 commit b14692a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
34 changes: 27 additions & 7 deletions app/src/main/java/com/example/quakereport/Earthquake.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package com.example.quakereport;

public class Earthquake {
/** 地震震级 */
/**
* 地震震级
*/
private double mag;

/** 地震位置(完整位置) */
/**
* 地震位置(完整位置)
*/
private String place;

/** Time of the earthquake */
/**
* Time of the earthquake
*/
private long timeInMilliseconds;

/**
* 地震的网站 URL
*/
private String url;

public double getMag() {
return mag;
}
Expand All @@ -25,16 +36,25 @@ public long getTimeInMilliseconds() {
return timeInMilliseconds;
}

/**
* 返回用于查找关于地震的更多信息的网站 URL。
*/
public String getUrl() {
return url;
}

/**
* 构造一个新的 {@link Earthquake} 对象。
*
* @param mag 表示地震的震级(大小)
* @param place 表示地震的城市位置
* @param timeInMilliseconds 表示地震发生时以毫秒(根据 Epoch)计的时间
* @param mag 表示地震的震级(大小)
* @param place 表示地震的城市位置
* @param timeInMilliseconds 表示地震发生时以毫秒(根据 Epoch)计的时间
* @param url 表示用于查找关于地震的更多详细信息的网站 URL
*/
public Earthquake(double mag, String place, long timeInMilliseconds) {
public Earthquake(double mag, String place, long timeInMilliseconds, String url) {
this.mag = mag;
this.place = place;
this.timeInMilliseconds = timeInMilliseconds;
this.url = url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/
package com.example.quakereport;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

Expand All @@ -39,10 +43,27 @@ protected void onCreate(Bundle savedInstanceState) {
ListView earthquakeListView = (ListView) findViewById(R.id.list);

// Create a new {@link ArrayAdapter} of earthquakes
EarthquakeAdapter adapter = new EarthquakeAdapter(this, earthquakes);
final EarthquakeAdapter adapter = new EarthquakeAdapter(this, earthquakes);

// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
earthquakeListView.setAdapter(adapter);

earthquakeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// 查找单击的当前地震
Earthquake currentEarthquake = adapter.getItem(position);

// 将字符串 URL 转换为 URI 对象(以传递至 Intent 中 constructor)
Uri earthquakeUri = Uri.parse(currentEarthquake.getUrl());

// 创建一个新的 Intent 以查看地震 URI
Intent websiteIntent = new Intent(Intent.ACTION_VIEW, earthquakeUri);

// 发送 Intent 以启动新活动
startActivity(websiteIntent);
}
});
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/com/example/quakereport/QueryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public static ArrayList<Earthquake> extractEarthquakes() {
// Extract the value for the key called "time"
long earthquakeTime = properties.getLong("time");

Earthquake earthquake = new Earthquake(earthquakeMag, earthquakePlace, earthquakeTime);
// 提取名为 "url" 的键的值
String earthquakeUrl = properties.getString("url");

// 使用震级、地点、时间和来自 JSON 响应的 url,
// 创建一个新的 {@link Earthquake} 对象。
Earthquake earthquake = new Earthquake(earthquakeMag, earthquakePlace, earthquakeTime, earthquakeUrl);

earthquakes.add(earthquake);
}
Expand Down

0 comments on commit b14692a

Please sign in to comment.