ArrayList being empty after adding elements

后端 未结 2 770
失恋的感觉
失恋的感觉 2020-12-01 23:31

I have this android code which bring a JSON from a server and fill an ArrayList from that JSON I checked the size of the ArrayList \"meals\" inside the onresponse void it gi

相关标签:
2条回答
  • 2020-12-01 23:35

    When you write this -

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

    That means you are making an asynchronous call and that mealsrequest will run on another thread. You are printing -

    Log.i("debug","meals ou size "+meals.size());
    

    just after you make your mealsrequest. When control reaches this statement your network request is not completed yet. So apparently, you don't have anything in your list. Your list will be populated in onResponse() only, since that method is executed after the network request gets completed.

    0 讨论(0)
  • 2020-12-01 23:50

    The problem here is about understanding how asynchronous tasks work. When you are adding a volley request to the queque, it will run in background thread (off the main thread) and control will pass to the next line.

    So, after this:

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

    control passes to this:

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

    Now since meals is updated on the background thread, you are not able to get the data by the time control reaches String mealsnames[]=new String[meals.size()];

    So you will get zero size (meals.size()) here.

    Try to move this portion of the code into onResponse.

    Try like this:

    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);
    }
    

    and call this method from 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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题