Android - updating Grid UI in debug mode but not updating at run time using Otto

162 Views Asked by At

I want to update the icon status from red to green after successfull upload of the image in background service. I am using Otto event bus to transfer data between Service to Activity, I can able to subscribe and get the data from Service to Activity after successful uploading of image. Then I am trying to update the Grid view of my custom adapter from activity, it's working fine in debug mode but not at run time. No clue why it happening like so..

What I am doing wrong? Any suggestion/clue will be appreciated.

Here is my event publishing code in Service:

The TaskBus.java:

public class TaskBus {

    public static Bus bus = new Bus();
}

In onStartCommand called thread to upload image:

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

Here is the thread to upload image in background:

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

Here is the code to send data to activity from Service after successful upload of image:

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

Here is the subscribe code to get data from Service in 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);
            }
        }

Here is my Adapter code to update grid view:

 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

There are 2 best solutions below

3
bajicdusko On

I would try to change two things.

  1. Make Bus a singleton and use the same instance in the whole app.
  2. Check the proguard settings and exclude TakingPhotoService.UploadStatusEvent from it and observe the results.

You haven't shown registration process in the activity, but since you have said it is working, I guess you did it. However, if you are not listening for events in the Service, there is no need to register bus in service. P.S. Don't forget to unregister it as well when activity is closed.

0
Shailendra Madda On

Before that I was called updateStatus(imageData); this before updating status in SQLite data base. Now I fixed this issue by just called updateStatus(imageData); after saving/updating the image data in local DB. Then problem solved.

Moved updateStatus(imageData); to end of the thread i.e., after saving in local DB fixed issue.

May it use for some one.