很抱歉,标题有点抽象。我试图从网站获取JSON代码并使用它初始化列表。但是,由于某种原因,该列表仍然是空的。我也是Android和Java的新手(字面意思是本周开始),所以建议和评论家都很感激!相关代码:
public class ForecastAdapter extends BaseAdapter {
public static String CallURL(final String URL) {
String line = "", all = "";
URL myUrl = null;
BufferedReader in = null;
try {
myUrl = new URL(URL);
in = new BufferedReader(new InputStreamReader(myUrl.openStream()));
while ((line = in.readLine()) != null)
all += line;
} catch (Exception e) {
e.printStackTrace();}
finally {
if (in != null) {
in.close();
}
}
return all;
}
private List<ForecastDataSet> getDataForListView()
{
//ForecastDataSet is composed of 6 strings: Day, TempAvg, TempMin, TempMax, WeatherState and IconID
List<ForecastDataSet> dataList = new ArrayList<ForecastDataSet>();
String RawData = null;
try {
//Try to collect weather API data
RawData = CallURL("http://api.openweathermap.org/data/2.5/forecast/daily?q=Berlin,de&units=metric&cnt=16");
} catch (Exception e) {
Log.e("Mini Weather Error", "CallURL method failed: " + e.getMessage());
}
//Parse RawData and extract necessary information
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(RawData);
JSONObject jsonObject = (JSONObject) obj;
//Iterate through the elements inside "list" in the JSON file
ForecastDataSet data = new ForecastDataSet();
JSONArray list = (JSONArray)jsonObject.get("list");
Iterator i = list.iterator();
//Fill the ForecastDataSet and add it to the bigger list (dataList)
int increment = 0;
while (i.hasNext()) {
JSONObject innerObj = (JSONObject) i.next();
JSONObject temps = (JSONObject) innerObj.get("temp");
JSONArray tempList = (JSONArray) innerObj.get("weather");
JSONObject weatherStatus = (JSONObject) tempList.get(0);
data.Day = getDay(increment);
data.TempAvg = ((String) temps.get("day")).split(".")[0] + " °C";
data.TempMin = ((String) temps.get("min")).split(".")[0] + " °C";
data.TempMax = ((String) temps.get("max")).split(".")[0] + " °C";
data.WeatherState = (String) weatherStatus.get("main");
data.IconID = (String) weatherStatus.get("icon");
dataList.add(data);
++increment;
}
return dataList;
} catch (Exception e) {
Log.e("Mini Weather Error", "Error while filling ForecastData: " + e.getMessage());
}
return null;
}
//Here is the list that appears to be empty even after being initialized
List<ForecastDataSet> ForecastData = getDataForListView();
@Override
public int getCount() {
try {
return ForecastData.size(); //-----> Always results in an error
}
catch (Exception e){
Log.e("Mini Weather Error", "ForecastData is empty");
}
return 0;
}
@Override
public ForecastDataSet getItem(int position) {
try {
return ForecastData.get(position); //-----> Always results in an error
}
catch (Exception e){
Log.e("Mini Weather Error", "ForecastData is empty");
}
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try{
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(parent.getContext());
View rowView = inflater.inflate(R.layout.list_item, parent, false);
}
//Fill a list item view and return it
TextView dayOfTheWeek = (TextView)convertView.findViewById(R.id.listItemDate);
TextView description = (TextView)convertView.findViewById(R.id.weather_info);
ImageView icon = (ImageView) convertView.findViewById(R.id.weatherIconItem);
ForecastDataSet currentData;
currentData = ForecastData.get(position);
dayOfTheWeek.setText(currentData.Day);
String fullDescription;
fullDescription = currentData.TempAvg + "\n\n" +
"Min: " + currentData.TempMin + "\n" +
"Max: " + currentData.TempMax;
description.setText(fullDescription);
return convertView;
}
catch (Exception ex)
{
Log.e("Mini Weather Error", "Something went wrong when creating the view, error message: \n" + ex.getMessage());
}
return null;
}
}
答案 0 :(得分:0)
因为,
List<ForecastDataSet> ForecastData = getDataForListView();
除非ForecastData
是静态函数,否则此行永远不会初始化getDataForListView()
列表。所以
创建ForecastAdapter
的构造函数并在其中调用getDataForListView();
。
像,
public class ForecastAdapter extends BaseAdapter {
List<ForecastDataSet> ForecastData;
public ForecastAdapter ()
{
//Here is the list that appears to be empty even after being initialized
ForecastData = getDataForListView();
}