Android - 在调试模式下更新Grid UI,但不使用Otto在运行时更新

时间:2017-02-27 07:07:52

标签: android otto

我想在后台服务中成功上传图片后,将图标状态从红色更新为绿色。我正在使用Otto事件总线在Service to Activity之间传输数据,我可以在成功上传图像后订阅并从Service获取数据。然后我尝试从活动更新我的自定义适配器的网格视图,它在调试模式下正常工作但在运行时没有。不知道为什么会这样发生 ..

我做错了什么?任何建议/线索将不胜感激。

以下是我在服务中发布代码的事件:

TaskBus.java:

public class TaskBus {

    public static Bus bus = new Bus();
}

在onStartCommand调用线程上传图片:

    // start thread to upload image
       new Thread(ImageUpload).start();

以下是在后台上传图片的主题:

    public Runnable ImageUpload = new Runnable() {

            public void run() {
                try {
                    response = uploadImageToServer(server, imageData);


    if (!TextUtils.isEmpty(response)) {
                        showToastMessage("Image uploaded successfully.");
// update status icon in UI
                    updateStatus(imageData);
    // save/update in local DB
    // save/Update code goes here to save status in local DB

                } catch (Exception ex) {
                    ex.printStackTrace();
                    showToastMessage("Problem occurred while uploading image.");

                }

            }
        };

    private void showToastMessage(final String message) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                }
            });
        }

以下是成功上传图片后从服务发送数据的代码:

private void updateStatus(ImageData data) {
        final UploadStatusEvent uploadStatusEvent = new UploadStatusEvent();
        uploadStatusEvent.data = data;
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                bus.post(uploadStatusEvent);
            }

        });
    }

public class UploadStatusEvent {
        public ImageData data;
    }

以下是从Activity中获取数据的订阅代码:

// update status icons , it called when image uploaded successfully in Service in background

        @Subscribe
        public void receiveTakingPhotoServiceUploadImageStatus(TakingPhotoService.UploadStatusEvent uploadStatusEvent) {
            ImageData data = uploadStatusEvent.data;

            if (data != null) {
                imageAdapter.setImageInGridItem(data);
                gridView.invalidateViews();
                gridView.setAdapter(imageAdapter);
            }
        }

这是我的适配器代码,用于更新网格视图:

 public void setImageInGridItem(ImageData data) {
        try {
            GridItem gridItem = getItem(data.position);

            gridItem.setStatus(data.status);

            gridItems.remove(vehicleImage.position);
            gridItems.add(ImageData.position, gridItem);
            setNotifyOnChange(true);
            this.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

2 个答案:

答案 0 :(得分:0)

我会尝试改变两件事。

  1. 让Bus成为单身人士并在整个应用程序中使用相同的实例。
  2. 检查proguard设置并从中排除TakingPhotoService.UploadStatusEvent并观察结果。
  3. 您尚未在活动中显示注册过程,但由于您说它正在运行,我猜您已经这样做了。但是,如果您没有在服务中监听事件,则无需在服务中注册总线。 附:在活动结束时,不要忘记取消注册。

答案 1 :(得分:0)

在此之前,在更新SQLite数据库中的状态之前,我被称为updateStatus(imageData);。现在我通过调用updateStatus(imageData)修复了这个问题。在本地DB中保存/更新图像数据后。然后问题解决了。

  

updateStatus(imageData);移至线程的末尾,即保存在本地数据库中的固定问题后。

可以用于某个人。