迭代异步事件,需要传递一个对象

时间:2015-05-15 22:53:20

标签: java android

我有以下问题:

我已经为我的服务器返回对象实现了一个API,这一切都很好。对API的所有调用都会启动AsyncTask,并在方法中返回结果。问题是,在返回的事件列表中,有一点(我需要这是错误的编码,但忽略了一分钟)。这些事件是从一个AsyncTask调用返回的,并用于另一个AsyncTask调用,我需要将我正在迭代的事件传递给下一个API调用,如下所示:

api.retrieveEvents(new GetResponseCallback<Event>() {
        @Override
        public void onDataReceived(ArrayList<Event> list) {
            for (Event e : list) {
                api.retrieveReservationsForEvent(e, new GetResponseCallback<EventRegistration>() {
                    @Override
                    public void onDataReceived(ArrayList<EventRegistration> list) {
                        Log.d("We are in event", e.getEventName());
                    }
                });

现在很明显,Java不会允许你在类范围之外使用局部变量,而不会将其声明为final。这显然不会在这里工作,我也无法将事件分配给字段变量,因为这只会导致我正在迭代的最终事件成为retrieveReservationsForEvent调用所有用途的事件。关于该怎么做的任何想法?

retrieveReservationsForEvent看起来像这样:

public void retrieveReservationsForEvent(Event event, final GetResponseCallback<EventRegistration> callback) {
    final ArrayList<EventRegistration> eventRegistrations = new ArrayList<>();
    String restUrl = SERVER_NAME + REST_EVENTREGISTRATION + event.getId();
    new GetTask(restUrl, new RestTaskCallback() {
        @Override
        public void onComplete(String response) {
            try {
                // TODO probably throws an exception if there is only one attendant...
                JSONArray jsonArray = new JSONArray(response);
                for(int i = 0; i < jsonArray.length(); i++) {
                    JSONObject tempEventRegistrationJSON = (JSONObject) jsonArray.get(i);

                    long dateInMillis = tempEventRegistrationJSON.getLong("timeOfArrival");
                    Date date = new Date(dateInMillis);

                    EventRegistration tempEventRegistration = new EventRegistration(tempEventRegistrationJSON.getInt("id"),
                            tempEventRegistrationJSON.getInt("eventNightId"), tempEventRegistrationJSON.getInt("guestId"),
                            date, tempEventRegistrationJSON.getInt("numberOfGuests"));
                    tempEventRegistration.setGuestName(tempEventRegistrationJSON.getString("guestName"));
                    tempEventRegistration.setGuestPhoneNumber(tempEventRegistrationJSON.getInt("phoneNumber"));
                    tempEventRegistration.setGuestMail("mail");

                    eventRegistrations.add(tempEventRegistration);

                    callback.onDataReceived(eventRegistrations);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }).execute();
}

GetTask只是在做一个GET请求,返回响应。

1 个答案:

答案 0 :(得分:1)

您确定将其更改为:

function mutation(str1, str2){
  for (var i = 0; i < str2.length; i++) {
    var index = str1.indexOf(str2.charAt(i));
    if (index == -1) return false;
    str1 = str1.substr(0, index) + str1.substr(index + 1);
  }
  return true;
}

不起作用?编译时是否会出错?