I have a snippet that contains a request to display information about the user's groups.

The problem is that if I print an arrayid outside of the query, then an empty arrayid . And if the request is all good. What could be the reason? Thank!

 public class fragment extends Fragment { private static final int LAYOUT = R.layout.fragment; private View view; ArrayList<String> arrayid = new ArrayList<>(); @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(LAYOUT,container,false); VKRequest vkRequest = VKApi.groups().get(VKParameters.from(VKApiConst.EXTENDED, 1, "name")); vkRequest.executeWithListener(new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { super.onComplete(response); System.out.println(response.responseString); try { JSONObject jsonObject = (JSONObject) response.json.get("response"); JSONArray jsonArray = (JSONArray) jsonObject.get("items"); System.out.println("ssshsh"+jsonArray); for (int i = 0; i<jsonArray.length();i++){ JSONObject name = (JSONObject) jsonArray.get(i); arrayid.add(name.getString("id")); } } catch (JSONException e) { e.printStackTrace(); } //если вывожу тут, то все работает System.out.println(Arrays.asList(arrayid)); } }); //если вывожу тут то выводится пустой arrayid } } 

    1 answer 1

    The reason is that in the case of output of arrayid to the console outside of the onComplete(...) arrayid , this arrayid is not yet filled with data.

    You perform an asynchronous request, in which you fill with arrayid data. But at the time of the output of the arrayid request has not yet been executed, hence the arrayid will be empty.

    • how can i fix it - Donald Rump
    • @DonaldRump, Display the data after the request is executed. Those. in the onComplete(...) callback. - post_zeew
    • @DonaldRump and why do you need to fix this? Output to onComplete and do not suffer, the query should be formed so that your arrayid filled - Iman
    • Well, for example, if I want to use this arrayid in other methods and queries. Are there any other options ? - Donald Rump
    • @DonaldRump, After executing the query, you can run another method that will do something further. - post_zeew