我有一个返回arraylist的方法,它是从jsonarray填充的 jsonrequest工作正常,但是当我调用该方法时,arraylist返回大小为0,即使它被填充。 下面是代码,我做错了什么
private ArrayList<String> queryRes(String url, String searchString) {
final ArrayList<String> mylist = new ArrayList<>();
AsyncHttpClient client = new AsyncHttpClient();
client.get(url + searchString,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONArray jsonArray) {
for(int i =0; i < jsonArray.length(); i++) {
mylist.add(i, jsonArray.optJSONObject(i).optString("id"));
//Check if it is added to the list
Log.d("Try: ", mylist.get(i));
}
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
}
});
return mylist;
}
答案 0 :(得分:0)
您可以在调试器上验证它
我的建议是从onSuccess()调用一个方法,这样你就会得到Arraylist
答案 1 :(得分:0)
如果你在一个班级里做所有事情
public void populateList(String url, String searchString)
{
final ArrayList<String> mylist = new ArrayList<>();
AsyncHttpClient client = new AsyncHttpClient();
client.get(url + searchString,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONArray jsonArray) {
for(int i =0; i < jsonArray.length(); i++) {
mylist.add(i, jsonArray.optJSONObject(i).optString("id"));
//Check if it is added to the list
Log.d("Try: ", mylist.get(i));
}
onListPopulated(myList);
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
}
});
}
使用列表
执行某些操作的方法public void onListPopulated(ArrayList<String> list)
{
//do something with the list
}