我想用来自 JSON 链接的数据填充网格视图。检索数据后如何填充网格视图。它基本上是一个天气应用程序。
这是我的 JSON 解析器类:
public class JSONWeatherParser {
public static WeatherForecast getForecastWeather(String data) throws JSONException {
WeatherForecast forecast = new WeatherForecast();
// We create out JSONObject from the data
JSONObject jObj = new JSONObject(data);
JSONArray jArr = jObj.getJSONArray("list"); // Here we have the forecast for every day
// We traverse all the array and parse the data
for (int i=0; i < jArr.length(); i++) {
JSONObject jDayForecast = jArr.getJSONObject(i);
// Now we have the json object so we can extract the data
DayForecast df = new DayForecast();
// We retrieve the timestamp (dt)
df.timestamp = jDayForecast.getLong("dt");
// Temp is an object
JSONObject jTempObj = jDayForecast.getJSONObject("temp");
df.forecastTemp.day = (float) jTempObj.getDouble("day");
df.forecastTemp.min = (float) jTempObj.getDouble("min");
df.forecastTemp.max = (float) jTempObj.getDouble("max");
df.forecastTemp.night = (float) jTempObj.getDouble("night");
df.forecastTemp.eve = (float) jTempObj.getDouble("eve");
df.forecastTemp.morning = (float) jTempObj.getDouble("morn");
// Pressure & Humidity
df.weather.currentCondition.setPressure((float) jDayForecast.getDouble("pressure"));
df.weather.currentCondition.setHumidity((float) jDayForecast.getDouble("humidity"));
// ...and now the weather
JSONArray jWeatherArr = jDayForecast.getJSONArray("weather");
JSONObject jWeatherObj = jWeatherArr.getJSONObject(0);
df.weather.currentCondition.setWeatherId(getInt("id", jWeatherObj));
df.weather.currentCondition.setDescr(getString("description", jWeatherObj));
df.weather.currentCondition.setCondition(getString("main", jWeatherObj));
df.weather.currentCondition.setIcon(getString("icon", jWeatherObj));
forecast.addForecast(df);
}
return forecast;
}
private static JSONObject getObject(String tagName, JSONObject jObj) throws JSONException {
JSONObject subObj = jObj.getJSONObject(tagName);
return subObj;
}
private static String getString(String tagName, JSONObject jObj) throws JSONException {
return jObj.getString(tagName);
}
private static float getFloat(String tagName, JSONObject jObj) throws JSONException {
return (float) jObj.getDouble(tagName);
}
private static int getInt(String tagName, JSONObject jObj) throws JSONException {
return jObj.getInt(tagName);
}
}
GridViewAdapter 类:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
private int numDays;
private WeatherForecast forecast;
private final static SimpleDateFormat sdf = new SimpleDateFormat("E, dd-MM");
private DayForecast dayForecast;
private ImageView iconWeather;
public GridViewAdapter(int numDays, WeatherForecast forecast) {
this.numDays = numDays;
this.forecast = forecast;
}
public int getCount() {
return numDays;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
//ImageView imageView;
//ImageView icon;
TextView temp;
TextView date;
if (convertView == null) { // if it's not recycled, initialize some attributes
//icon = new ImageView(mContext);
temp = new TextView(mContext);
date = new TextView(mContext);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_icon, parent, false);
}
temp=(TextView)convertView.findViewById(R.id.text);
temp.setText( (int) (dayForecast.forecastTemp.min - 275.15) + "-" + (int) (dayForecast.forecastTemp.max - 275.15) );
date=(TextView)convertView.findViewById(R.id.date);
date.setText(DateUtils.formatDateTime(mContext, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE ));
return convertView;
//JSONIconWeatherTask task = new JSONIconWeatherTask();
//task.execute(new String[]{dayForecast.weather.currentCondition.getIcon()});
/*imageView.setImageResource(mThumbIds[position]);*/
//return imageView;
}
public void setForecast(DayForecast dayForecast) {
this.dayForecast = dayForecast;
}
我的异步任务:
private class JSONForecastWeatherTask extends AsyncTask<String, Void, WeatherForecast> {
@Override
protected WeatherForecast doInBackground(String... params) {
String data = ( (new WeatherHttpClient()).getForecastWeatherData(params[0], params[1], params[2]));
WeatherForecast forecast = new WeatherForecast();
try {
forecast = JSONWeatherParser.getForecastWeather(data);
System.out.println("Weather ["+forecast+"]");
} catch (JSONException e) {
e.printStackTrace();
}
return forecast;
}
@Override
protected void onPostExecute(WeatherForecast forecastWeather) {
super.onPostExecute(forecastWeather);
GridView gridView;
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new GridViewImageAdapter(Integer.parseInt(forecastDaysNum), forecastWeather));
}
} 日志
09-27 00:03:10.132:E/AndroidRuntime(29848):致命异常:主要 09-27 00:03:10.132:E/AndroidRuntime(29848):java.lang.NullPointerException 09-27 00:03:10.132: E/AndroidRuntime(29848): 在 android.app.Activity.findViewById(Activity.java:1843) 09-27 00:03:10.132: E/AndroidRuntime(29848): 在 com.example.weatherforecast.MainActivity$JSONForecastWeatherTask.onPostExecute (MainActivity.java:205) 09-27 00:03:10.132: E/AndroidRuntime(29848): 在 com.example.weatherforecast.MainActivity$JSONForecastWeatherTask.onPostExecute(MainActivity.java:1) 09-27 00:03:10.132 : E/AndroidRuntime(29848): 在 android.os.AsyncTask.finish(AsyncTask.java:631) 09-27 00:03:10.132: E/AndroidRuntime(29848): 在 android.os.AsyncTask.access$600(AsyncTask .java:177) 09-27 00:03:10.132: E/AndroidRuntime(29848): 在 android.os.AsyncTask$InternalHandler。handleMessage(AsyncTask.java:644) 09-27 00:03:10.132: E/AndroidRuntime(29848): 在 android.os.Handler.dispatchMessage(Handler.java:99) 09-27 00:03:10.132: E/ AndroidRuntime(29848): 在 android.os.Looper.loop(Looper.java:137) 09-27 00:03:10.132: E/AndroidRuntime(29848): 在 android.app.ActivityThread.main(ActivityThread.java:5234 ) 09-27 00:03:10.132: E/AndroidRuntime(29848): at java.lang.reflect.Method.invokeNative(Native Method) 09-27 00:03:10.132: E/AndroidRuntime(29848): at java. lang.reflect.Method.invoke(Method.java:525) 09-27 00:03:10.132: E/AndroidRuntime(29848): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799 ) 09-27 00:03:10.132: E/AndroidRuntime(29848): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 09-27 00:03:10.132: E/AndroidRuntime(29848 ):在达尔维克。system.NativeStart.main(本机方法)
我仍然是Java的初学者。