我有这个android代码,它从服务器中获取JSON并从该JSON填充ArrayList
我在onresponse无效内部检查了ArrayList“ meals”的大小,它给了我1,但是当我在StringRequest对象之后对其进行检查时,我得到了0个项目。
进餐在全局范围内定义,并在oncreateview函数内部初始化
代码:

               public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.i("debug","on create view");
View view =inflater.inflate(R.layout.fragment_meal_list,container,false);
ListView List ;
meals=new ArrayList<meal>();
String url="http://syriankitchen.tk/get_recent.php";
StringRequest mealsrequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

    @Override
    public void onResponse(String response) {
        try{
            JSONObject object= new JSONObject(response);
            JSONArray mealsArray = object.getJSONArray("result");
            for(int i=0;i<mealsArray.length();i++){
                JSONObject cur = mealsArray.getJSONObject(i);
                int id= cur.getInt("id");
                String name= cur.getString("name");
                String description = cur.getString("description");
                int price = cur.getInt("price");
                meals.add(new meal(id,name,price,description));
            }
            Log.i("debug","meals size = "+meals.size());
        }
        catch(JSONException e){
            e.printStackTrace();
        }
    }
},new Response.ErrorListener(){
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getActivity(),error.getMessage(),Toast.LENGTH_SHORT).show();
    }
});
Volley.newRequestQueue(getActivity()).add(mealsrequest);
ArrayList<String> strs=new ArrayList<String>();
String mealsnames[]=new String[meals.size()];
for(int i=0;i<meals.size();i++)strs.add(meals.get(i).getName());
strs.toArray(mealsnames);
Log.i("debug","meals ou size "+meals.size());
CustomList  adapter = new CustomList(getActivity(),mealsnames,meals);
List = (ListView)view.findViewById(R.id.list);
List.setAdapter(adapter);

最佳答案

这里的问题是关于了解asynchronous任务如何工作的。当您向队列中添加一个volley请求时,该请求将在后台线程(不在主线程中)中运行,并且控制权将传递到下一行。

因此,在此之后:

Volley.newRequestQueue(getActivity()).add(mealsrequest);


控制传递给此:

ArrayList<String> strs=new ArrayList<String>();
String mealsnames[]=new String[meals.size()];


现在,由于meals在后台线程上进行了更新,因此您无法在控件达到String mealsnames[]=new String[meals.size()];时获取数据。

因此,您将在此处获得零大小(meals.size())。

尝试将这部分代码移到onResponse中。

尝试这样:

public void updateData(){
   ArrayList<String> strs=new ArrayList<String>();
   String mealsnames[]=new String[meals.size()];
   for(int i=0;i<meals.size();i++)strs.add(meals.get(i).getName());
   strs.toArray(mealsnames);
   Log.i("debug","meals ou size "+meals.size());
   CustomList  adapter = new CustomList(getActivity(),mealsnames,meals);
   List = (ListView)view.findViewById(R.id.list);
   List.setAdapter(adapter);
}


并从onResponse调用此方法:

@Override
public void onResponse(String response) {
    try{
        JSONObject object= new JSONObject(response);
        JSONArray mealsArray = object.getJSONArray("result");
        for(int i=0;i<mealsArray.length();i++){
            JSONObject cur = mealsArray.getJSONObject(i);
            int id= cur.getInt("id");
            String name= cur.getString("name");
            String description = cur.getString("description");
            int price = cur.getInt("price");
            meals.add(new meal(id,name,price,description));
        }
        Log.i("debug","meals size = "+meals.size());
        updateData();
    }
    catch(JSONException e){
        e.printStackTrace();
    }
}

09-11 18:10