Java Question Only the original thread that created a view hierarchy can touch its views.

barx

Well-Known Member
Licensed User
Longtime User
I had the code below minus the
B4X:
mBA.activity.runOnUiThread(new Runnable() {

}

In B4A I could get the size of the List but not access the maps, I got the error (see title)

After searching for info on the error I found THIS thread to help. I implemented the runOnUIThread, which is a method of Activity (so used ba.Activity in this case). Also Added the couple of bits of logging to see what is going on. I still get the 'DataChanged' log, but nothing else. indicating that it's simply not working right.

B4X:
import anywheresoftware.b4a.objects.collections.List;
import anywheresoftware.b4a.objects.collections.Map;

....
....

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        BA.Log("DataChanged");
       
        final java.util.List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
        dataEvents.close();
       
        mBA.activity.runOnUiThread(new Runnable() {
            @Override
            public void run(){
                BA.Log("Inside Run");
                if (mBA.subExists(mEventname + "_datachanged")) {
                    //Get the data
           
                    List Items = new List();
                    List DeletedItems = new List();
                    Items.Initialize();
                    DeletedItems.Initialize();
                   
                    for (DataEvent event : events) {
               
                        if (event.getType() == DataEvent.TYPE_CHANGED) {
                            Map Item  = new Map();
                            Item.Initialize();
                            DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                            Item.Put(dataMapItem.getUri().getPath(), dataMapItem.getDataMap())  ;
                            Items.Add(Item.getObject());
                        } else if (event.getType() == DataEvent.TYPE_DELETED) {
                            Map Item = new Map();
                            Item.Initialize();
                            DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                            Item.Put(dataMapItem.getUri().getPath(), dataMapItem.getDataMap());
                            DeletedItems.Add(Item.getObject());
                        }
                    }
                    BA.Log("Raising Event");
                    mBA.raiseEvent(mGoogleApiClient, mEventname + "_datachanged", new Object[] {Items, DeletedItems});
                }
            }
        });
       
    }

Running out of ideas again, so if anyone has any clues???? :confused:
 

barx

Well-Known Member
Licensed User
Longtime User
hmmm, good point. What's a cracking then?
 

barx

Well-Known Member
Licensed User
Longtime User
Actually, I have an idea. Now that I am using runOnUIThread I think I'm back to trying to raise an event from another thread....

Back to testing
 

barx

Well-Known Member
Licensed User
Longtime User
YAY, I believe I have fixed it.

No need for the runOnUiThread(). A simple raiseEventFromDifferentThread seems to have solved the issue again.....

Whoop, whoop

Now I can add DataItems and actually get them out the other end lol.
 
Top