gpt4 book ai didi

java - 无法将 json 数据添加到 ArrayList 中

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:05 25 4
gpt4 key购买 nike

我试图在 listView 中显示来自 JSON 的数据,但每当我尝试从 json 添加一些值时,我的列表都不会显示任何内容。

Activity

public static final String LOG = hourlyWeather.class.getSimpleName();
double test;
RequestQueue mQuene;
ArrayList<Weather> list;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hourly_weather);
mQuene = Volley.newRequestQueue(this);

list = new ArrayList<Weather>();
test();

hourlyWeatherAdapter adapter = new hourlyWeatherAdapter(this, list);

ListView listView = (ListView) findViewById(R.id.weatherListView);

listView.setAdapter(adapter);
}
public void test() {
String url = "https://api.openweathermap.org/data/2.5/weather?lat=50.7578594&lon=16.2127653&units=metric&appid=ID";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
try {
JSONObject object = response.getJSONObject("main");
for (int i = 0; i<object.length(); i++){
test = object.getDouble("temp");
}
Log.i(LOG,String.valueOf(test));
list.add(new Weather(test));

} catch (JSONException e) {
e.printStackTrace();
}
}, Throwable::printStackTrace);
mQuene.add(request);
}
}

适配器

public class hourlyWeatherAdapter extends ArrayAdapter<Weather> {

public hourlyWeatherAdapter(@NonNull Context context, ArrayList<Weather> list) {
super(context, 0, list);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View view = convertView;
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.weather_list_item, parent, false);
}
Weather model = getItem(position);

TextView temperature = (TextView) view.findViewById(R.id.hourly_time);
temperature.append(String.valueOf(model.getTemp()));

return view;
}
}

public class Weather {

private double temp;

public Weather(double temp) {
this.temp = temp;
}

public double getTemp() {
return temp;
}
}

当我使用 Log.i 时,我可以清楚地看到 logcat 中 json 的值。哪里可以出问题?我尝试添加一些虚拟数据,效果很好。

最佳答案

您必须在 onCreate() 方法外部声明 hourlyWeatherAdapter adapter = new hourlyWeatherAdapter(this, list);,并且在更改数组后,必须调用 notifyDataSetChanged();。此外,您必须在调用 test() 之前初始化列表和适配器。

public static final String LOG = hourlyWeather.class.getSimpleName();
double test;
RequestQueue mQuene;
ArrayList<Weather> list;
hourlyWeatherAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hourly_weather);
mQuene = Volley.newRequestQueue(this);

list = new ArrayList<Weather>();
adapter = new hourlyWeatherAdapter(this, list);

ListView listView = (ListView) findViewById(R.id.weatherListView);
listView.setAdapter(adapter);

test();
}

public void test() {
String url = "https://api.openweathermap.org/data/2.5/weather?lat=50.7578594&lon=16.2127653&units=metric&appid=ID";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
try {
JSONObject object = response.getJSONObject("main");
for (int i = 0; i<object.length(); i++){
test = object.getDouble("temp");
}
Log.i(LOG,String.valueOf(test));
list.add(new Weather(test));

// Update list
adapter.notifyDataSetChanged();

} catch (JSONException e) {
e.printStackTrace();
}
}, Throwable::printStackTrace);
mQuene.add(request);
}
}

关于java - 无法将 json 数据添加到 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56382774/

25 4 0