正在填充凌空onResponse回调方法里面后空的ArrayList [英] arraylist empty after being populated inside Volley onResponse callback method
问题描述
我面临一个很奇怪的issue..i填充字符串值一个ArrayList中凌空库onResponse方法...测井内onResponse ArrayList的值是显示出ArrayList是不是空的......但是当我做同前ArrayList是从方法reuturned是空的...
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以及搜索bit..i后,发现了一个solution..what我没有解决的问题是建立一个回调接口,并执行当onResponse已经完成...
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(终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被调用接口的一个新对象,这是完成我填充一个ArrayAdapter唯一onResponse后执行回调方法里面执行...
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
}
});
这篇关于正在填充凌空onResponse回调方法里面后空的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!