我有一个静态列表列表,当尝试在onResponse排球方法中从服务器填充此列表时,它可以工作,但在Response.Listener范围之外,列表变为空
public class MapsFragment extends Fragment implements OnMapReadyCallback {
static List<Train> trainsListe = new ArrayList<>();
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
public void parseRedis() {
StringRequest stringRequest = new StringRequest(uri2,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
List<Train> listeTemporaire = TrainRedisJsonParser.parseData(response);
for (Train t : listeTemporaire)
MapsFragment.trainsListe.add(t);
System.out.println(trainsListe.size()); // size is 4 here (it works)
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Toast.makeText(getActivity(),error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
parseRedis();
System.out.println(MapsFragment.trainsListe.size()) // 0
}
但是在方法之外,例如在onViewCreated的某处,当我尝试打印trainListe的大小时:0