0
我想在ArrayList中使用String數據。我試圖從OpenWeatherMap API檢索預測數據(採用JSON格式)。 「我將數據存儲在JSONObject中,然後把它放在字符串中,現在我想在ArrayList的幫助下顯示數據,但是我無法在ArayList中檢索數據,我使用LogCat調試代碼,發現所有的String都有數據但ArrayList沒有獲取數據。「 任何人都可以告訴我我在做什麼wrong.Below是我的OnpostExecute方法的代碼。謝謝你的幫助。如何使用帶有散列表的ArrayList
protected void onPostExecute(String file_url) {
String gradfix = city.replace("%20", " ");
grad.setText(gradfix);
pb.setVisibility(View.GONE);
grad.setVisibility(View.VISIBLE);
try {
// Getting Array of Contacts
JSONObject json2 = new JSONObject(file_url);
JSONArray forecast = null;
forecast = json2.getJSONArray("list");
// looping through All Contacts
for(int i = 0; i < forecast.length(); i++){
JSONObject c = forecast.getJSONObject(i);
String dt = c.getString("dt");
String day = c.getString("day");
Log.e("day",day.toString());
String min = c.getString("min");
String max = c.getString("max");
String pressure = c.getString("pressure");
String humidity = c.getString("humidity");
JSONArray weather = c.getJSONArray("weather");
for(int index = 0; index < weather.length(); index++){
JSONObject weatherObject = weather.getJSONObject(index);
String clouds = weatherObject.getString("clouds");
String speed = weatherObject.getString("speed");
String deg = weatherObject.getString("deg");
}
}
final ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
for (HashMap<String, Object> map : list) {
//HashMap<String, Object> map = new HashMap<String, Object>();
//Log.e("list",pressure.toString());
double srednja = Double.parseDouble(map.get("day").toString());
double ssrednja2 = (double) Math.floor(srednja);
srednja1.setText(format.format(ssrednja2)+"°F");
tlak.setText(map.get("pressure").toString()+" hpa");
tlak.setVisibility(View.VISIBLE);
vlaznost.setText(map.get("humidity").toString()+" %");
vlaznost.setVisibility(View.VISIBLE);
vjetar.setText(map.get("speed").toString()+" m/s");
vjetar.setVisibility(View.VISIBLE);
String imgrestring = map.get("icon").toString();
Bitmap slika = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("img"+imgrestring, "drawable", getPackageName()));
img_mid.setImageBitmap(slika);
Float stupnjevi = Float.valueOf(map.get("deg").toString());
Log.d("jea", String.valueOf(stupnjevi));
Bitmap bmpOriginal = BitmapFactory.decodeResource(context.getResources(), R.drawable.wind);
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(stupnjevi, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
BitmapDrawable mDrawable = new BitmapDrawable(getResources(),bmResult);
vjetar.setCompoundDrawablesWithIntrinsicBounds(null, null, mDrawable, null);
stanje.setText(map.get("main").toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
'爲(HashMap中<字符串,對象>圖:列表){'您正在迭代您剛剛創建的列表。它是空的。你還期望什麼? – njzk2
最好的選擇是將JSON數據反序列化成一些定製的Java類。或者,在你目前的方法中,你可以在weather.length()循環內做你需要的事情 - 因爲在這裏迭代了實際的天氣信息對象。 – ishmaelMakitla
你的'ArrayList> list'是空的,只有初始化,因此你永遠不會輸入for循環。 –
Drez