同步列表的对象不能从接口回调更改

时间:2015-05-15 11:52:40

标签: java android

预计完全是迷你。

我正在使用方法,它在另一个线程中向服务器发出请求:

    getGeocodingResults(finalDepartureUrl, new IVolleyCallback() {
                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        handleVolleyResponse(jsonObject, order, 1);
                        OrdersFragment.syncedOrdersList.get(OrdersFragment.syncedOrdersList.indexOf(order)).setDeparturePoint(order.getDeparturePoint());
                        OrdersFragment.syncedOrdersList.set(OrdersFragment.syncedOrdersList.indexOf(order), order);

                });

onSuccess moght要在主线程中调用,我将新值设置为静态同步列表。这是一个声明:

syncedOrdersList  = Collections.synchronizedList(new ArrayList(ordersList)); 

但是当我在回调执行后检查同一个对象时,我看到,该字段的新值未设置 - 因此departurePoint字段为空。

我做错了什么?

方法,我遍历所有项目并在每个循环中调用backgroung操作:

 public void addOrdersToMap(LinkedBlockingQueue<Order> list)
{
    if (ordersToDisplay != null)
        clearMap();
    if (list != null && !list.isEmpty()) {
        for (final Order order : list) {
            String finalDepartureUrl = Constants.URL_GEOCODING + getAddressUrlString(Constants.FLAG_ADDRESS_DEPARTURE,order);
            String finalDestinationUrl = Constants.URL_GEOCODING + getAddressUrlString(Constants.FLAG_ADDRESS_DESTINATION, order);
            getGeocodingResults(finalDepartureUrl, new IVolleyCallback() {
                @Override
                public void onSuccess(JSONObject jsonObject) {
                    handleVolleyResponse(jsonObject, order, Constants.FLAG_ADDRESS_DEPARTURE);
                }
            });
            getGeocodingResults(finalDestinationUrl, new IVolleyCallback() {
                @Override
                public void onSuccess(JSONObject jsonObject) {
                    handleVolleyResponse(jsonObject, order, Constants.FLAG_ADDRESS_DESTINATION);
                }
            });
               /*
                *  Log.i(TAG + " lat lng", a[0].toString());
                *  here the value of the setted field is null
                */
            }

    }

}

handleVolleyResponse是一个方法,我解析数据并将新值设置为对象的字段:

 private void handleVolleyResponse(JSONObject jsonObject, Order order, int flag) {
  /* ... here is json data parsing */
    switch (flag) {
       case Constants.FLAG_ADDRESS_DEPARTURE:
      /* here is a point, where I set parsed value into the  object's field */
             OrdersFragment.syncedOrdersList.get(OrdersFragment.syncedOrdersList.indexOf(order)).setDeparturePoint(latLng);
        order.setDeparturePoint(latLng);
        setMarker();
        break;
  case Constants.FLAG_ADDRESS_DESTINATION:
    /* here is a point, where I set parsed value into the object's field */
        OrdersFragment.syncedOrdersList.get(OrdersFragment.syncedOrdersList.indexOf(order)).setDestinationPoint(latLng);

  order.setDestinationPoint(latLng);
  setMarker();
   break;
 }

}

0 个答案:

没有答案