在 Volley onResponse 回调方法中填充后,arraylist 为空 [英] arraylist empty after being populated inside Volley onResponse callback method

查看:25
本文介绍了在 Volley onResponse 回调方法中填充后,arraylist 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个非常奇怪的问题..我在 Volley 库的 onResponse 方法中用字符串值填充数组列表...在 onResponse 中记录数组列表的值显示数组列表不为空...但是当我执行在从方法返回数组列表之前相同为空...

i am facing a very weird issue..i populate an arraylist with String values in onResponse method of Volley library...logging the values of arraylist inside onResponse is showing that arraylist is not empty...but when i do the same before the arraylist is reuturned from the method is empty...

private ArrayList<String> getFirmNamesToPopulateSpinner() {

        JsonArrayRequest jsonArrayFirmNamesRequest = new JsonArrayRequest(Request.Method.POST, AppConfig.URL_FIRMNAMES,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for(int i=0; i<response.length(); i++){
                            try {
                                JSONObject firmObject = response.getJSONObject(i);
                                String firmName = firmObject.getString(AppConfig.TAG_FIRM_NAME);
                                firmNamesArrayList.add(firmName);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        Log.d(debugTag, "return inside onResponse" + firmNamesArrayList);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(debugTag, "response error: " + error.getMessage());
                    }
                });
        Log.d(debugTag, "return before adding Request" + firmNamesArrayList);
        // Adding request to request queue
        VolleySingleton.getInstance().addToRequestQueue(jsonArrayFirmNamesRequest, AppConfig.TAG_FIRMNAMES_REQUEST);

        return firmNamesArrayList;
    }

谁能解释为什么会这样?

can anyone explain why is this happening?

推荐答案

根据 lordoku 并搜索了一下..我找到了一个解决方案..我为解决这个问题所做的是创建一个回调接口并执行onResponse完成时接口的回调方法getFirmNamesOnSuccess()...

according to lordoku and after searching a bit..i found out a solution..what i did to solve the issue was to create a callback interface and execute the callback method getFirmNamesOnSuccess() of the interface when the onResponse has been finished...

接口 FirmNamesRequestCallback

interface FirmNamesRequestCallback

private interface FirmNamesRequestCallback{
    void getFirmNamesOnSuccess(JSONArray firmName);
}

在 getFirmNamesToPopulateSpinner(final FirmNamesRequestCallback FirmNamesRequestCallback) 中,它以接口为参数被调用并且具体在onResponse里面执行接口的回调方法

inside getFirmNamesToPopulateSpinner(final FirmNamesRequestCallback firmNamesRequestCallback) which is being called with the interface as parameter and specifically inside onResponse the callback method of the interface is executed

 public void onResponse(JSONArray response) {
                    firmNamesRequestCallback.getFirmNamesOnSuccess(response);
                }

getFirmNamesToPopulateSpinner 使用接口的新对象和在回调方法的实现内部调用,该方法仅在 onResponse 完成后执行我填充 arrayadapter...

getFirmNamesToPopulateSpinner is called with a new object of the interface and inside the implementation of the callback method which is executed only after onResponse is finished i populate the arrayadapter...

 getFirmNamesToPopulateSpinner(new FirmNamesRequestCallback() {
                @Override
                public void getFirmNamesOnSuccess(JSONArray firmName) {
                    //do some stuff here the JSONArray response
                }
            });

这篇关于在 Volley onResponse 回调方法中填充后,arraylist 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆