通过使用凌空从方法返回ArrayList

时间:2019-04-15 16:12:48

标签: java android android-volley

当我返回arrayList为空时,我试图返回arrayList。当我返回arrayList但它不起作用时,我将使用它。

file(blocks) 2097151

2 个答案:

答案 0 :(得分:0)

您的问题是您试图返回尚未填充的内容,该调用是异步的,因此可能需要一些时间才能将日期填充到ArrayList<Cities>中,这就是为什么它为空的原因。

您应该删除此行return cities;,并记住在方法之外创建此final ArrayList<Cities> cities=new ArrayList<>();,并将返回的ArrayList<Cities>删除到void

然后,您可以创建一个回调以通知数据已填充到ArrayList<Cities>中,如果您尝试将其填充到RecyclerView中,我建议您在{{1 }}方法。

要进行回调,您可以查看此answer或此Gist

答案 1 :(得分:0)

如果响应返回带有City的City元素数组的JSONObject,则您的请求是正确的,但是如上所述,在调用回调之前,您不能指望实际得到结果。这是一个使用JSONArrayRequest并返回JSONArray的示例,但是从JSONObject中提取City数组后,其工作方式与您的工作原理几乎相同。

    private ListView mTripList;
public ArrayList<TripItem> tripItems;
private JSONArray unclaimedTrips;
private TripSelectAdapter adapter;



public void getTrips() {

        JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray> () {  

        @Override
        public void onResponse(JSONArray response) {

            // Public Array
            unclaimedTrips = response;

            tripItems = new ArrayList<TripItem>();


            // Optional if you want to manipulate the data in some way
            if (unclaimedTrips != null) {

                for (int i = 0; i < unclaimedTrips.length(); i++) {

                    try {
                        JSONObject item = unclaimedTrips.getJSONObject(i);

                        int tripID = item.getInt("trip_id");
                        int claimID = item.getInt("claim_id");
                        String payrollCode = item.getString("payroll_code");
                        String tripDate = item.getString("trip_date");
                        String tripPurpose = item.getString("trip_purpose");

                        TripItem tripItem = new TripItem(tripID, claimID, payrollCode, tripDate, tripPurpose);

                        tripItems.add(tripItem);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                refreshData();
            }

        }


    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            unclaimedTrips = null;
            refreshData();

        }
    }) {

       @Override
       public Map<String, String> getHeaders() throws AuthFailureError {
           HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Accept", "application/json");
                headers.put("Content-Type", "application/json");
           return headers;
       };
    };


    // add the request object to the queue to be executed
    MyApplication.getInstance().addToRequestQueue(req);
}


public void refreshData() {

    if (tripItems.isEmpty()) {
        mEmptyList.setVisibility(android.view.View.VISIBLE);
    } else {
        mEmptyList.setVisibility(android.view.View.GONE);                   
    }

    adapter = new TripSelectAdapter(this, thisContext, tripItems);
    mTripList.setAdapter(adapter);
}