【发布时间】:2018-09-01 11:43:57
【问题描述】:
我正在使用天气地下 API 构建天气应用程序。我有 10 天的预测列表视图,我想创建详细活动,以便在按下每个列表项后每天进行更详细的预测。而且我不知道如何将数据传递给详细活动。我将listView与适配器一起使用,并且我在带有maxtemp和min temp的列表上有图像url,并且在详细活动中我想要更详细的相同图像url我为两个图像视图(详细信息和listview)提供相同的ID,但它只能正常工作在名单上。我是否只在详细活动中实例化我的视图并使用 id 将它们附加到适配器?我尝试了一些额外的意图,但它不适用于数据 来自网络 api。
我确实像你说的那样,但在我的适配器中,我只放大了列表项视图,当我尝试启动应用程序时,我收到如下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
我的适配器类如下所示:
public class WeatherAdapter extends ArrayAdapter<Weather> {
public WeatherAdapter(Context context, ArrayList<Weather> weather) {
super(context, 0, weather);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Weather currentWeather = getItem(position);
TextView date = (TextView) listItemView.findViewById(R.id.month);
date.setText(currentWeather.getDate());
TextView minTemp = (TextView) listItemView.findViewById(R.id.low_temperature);
minTemp.setText(String.valueOf(currentWeather.getMinTemp()) + " \u2103");
TextView maxTemp = (TextView) listItemView.findViewById(R.id.high_temperature);
maxTemp.setText(String.valueOf(currentWeather.getMaxTemp()) + " \u2103");
ImageView image = (ImageView) listItemView.findViewById(R.id.weather_icon);
Picasso.with(getContext()).load(currentWeather.getUrl()).into(image);
TextView day = (TextView) listItemView.findViewById(R.id.day);
day.setText(String.valueOf(currentWeather.getDay()));
TextView weekday = (TextView) listItemView.findViewById(R.id.weekday);
weekday.setText(String.valueOf(currentWeather.getWeekday()));
TextView conditions = (TextView) listItemView.findViewById(R.id.conditions);
conditions.setText(currentWeather.getConditions());
TextView humidity = (TextView) listItemView.findViewById(R.id.humidity);
humidity.setText(String.valueOf(currentWeather.getHumidity()));
return listItemView;
}
}
我的适配器无法识别我的湿度 textView 所在的 activity_detail.xml,我不知道如何在我的适配器中扩展此布局
这是我的实用程序类
public class WeatherUtils {
private static final String LOG_TAG = WeatherUtils.class.getSimpleName();
public WeatherUtils() {
}
public static List<Weather> fetchNewsData(String requestUrl) throws JSONException {
URL url = createUrl(requestUrl);
String jsonResponse = null;
try {
jsonResponse = makeHttpsRequest(url);
} catch (IOException e) {
Log.e(LOG_TAG, "Problem making the HTTP request.", e);
}
List<Weather> weather = extractFromJSONResponse(jsonResponse);
return weather;
}
private static URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error with creating URL", e);
}
return url;
}
private static String makeHttpsRequest(URL url) throws IOException {
String jsonResponse = "";
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /*milliseconds*/);
urlConnection.setConnectTimeout(15000 /*milliseconds*/);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving Book JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader;
inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
private static List<Weather> extractFromJSONResponse(String JSONResponse) throws JSONException {
if (TextUtils.isEmpty(JSONResponse)) {
return null;
}
List<Weather> weather = new ArrayList<>();
try {
JSONObject jsonResponse = new JSONObject(JSONResponse);
JSONObject forecast = jsonResponse.getJSONObject("forecast");
JSONObject simpleForecast = forecast.getJSONObject("simpleforecast");
JSONArray listArray = simpleForecast.getJSONArray("forecastday");
for (int i = 0; i < listArray.length(); i++) {
JSONObject currentWeather = listArray.getJSONObject(i);
String iconUrl = currentWeather.getString("icon_url");
double humidity = currentWeather.getDouble("avehumidity");
JSONObject highTempObject = currentWeather.getJSONObject("high");
String maxTemp = highTempObject.getString("celsius");
JSONObject lowTempObject = currentWeather.getJSONObject("low");
String minTemp = lowTempObject.getString("celsius");
JSONObject dateObject = currentWeather.getJSONObject("date");
String date = dateObject.getString("monthname");
String weekday = dateObject.getString("weekday");
int day = dateObject.getInt("day");
int year = dateObject.getInt("year");
Weather data = new Weather(maxTemp, minTemp, humidity, date, iconUrl, year, day, weekday);
weather.add(data);
}
} catch(JSONException e){
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
return weather;
}
}
这是我的天气课
public class Weather {
private String maxTemp;
private String minTemp;
private double humidity;
private String date;
private int year;
private int day;
private String weekday;
private String url;
public Weather(String maxTemp, String minTemp, double humidity, String date, String url, int year, int day, String weekday) {
this.maxTemp = maxTemp;
this.minTemp = minTemp;
this.humidity = humidity;
this.date = date;
this.day = day;
this.year = year;
this.weekday = weekday;
this.url = url;
}
public String getMaxTemp() {
return maxTemp;
}
public String getMinTemp() {
return minTemp;
}
public double getHumidity() {
return humidity;
}
public int getYear() {
return year;
}
public int getDay() {
return day;
}
public String getDate() {
return date;
}
public String getUrl() {
return url;
}
public String getWeekday() {
return weekday;
}
}
【问题讨论】:
-
你的 Weather 课程有什么?
-
压力、湿度、温度、日期等数据的字段,具有基于这些字段的参数的构造函数和该字段的吸气剂
-
你在意图方面尝试过什么?
-
打算使用 putExtra mathod 和 EXTRA_TEXT 打开详细活动,但它不起作用
-
好的,用天气课更新你的答案,我会帮助你
标签: android json android-intent weather-api