spac3hit
spac3hit

Reputation: 201

Updating interface handler issue

So I have these lines of codes in my android app, wifiScrollViewText is of type String, I set to whatever message I want to append to the ViewText: wifiScrollViewText through the handler ... The readableNetmask in my case is 255.255.255.0 , and the readableIPAddress is 10.0.0.11 ... If I deletes Update 2 the Netmask will appear on the textview ... but if I add Update 2 line of codes, the textview will show the IP twice instead of Netmask then IPAddress. I think the solution is to wait for the first update to finish before starting the second handler Object!

// Update 1 
wifiScrollViewText = readableNetmask + "\n";
handler.post(new UpdateWiFiInfoTextViewRunnable());

// Update 2     
wifiScrollViewText = readableIPAddress + "\n";
handler.post(new UpdateWiFiInfoTextViewRunnable());

Runnable:

static public class UpdateWiFiInfoTextViewRunnable implements Runnable {
    public void run() {
        wifi_info_textView.append(wifiScrollViewText);
    }
}

Upvotes: 0

Views: 38

Answers (1)

user
user

Reputation: 87064

The two Runnables will not be run until the current message/code on the main thread is done executing, so by the time the two Runnables are run the wifiScrollViewText variable points to the same text. You'll need to hold the two pieces of text in either two separate variables or in a list(if you plan doing multiple appends) and pop them in a single run of the Runnable:

List<String> mUpdates = new ArrayList<String>();
// Update 1 
mUpdates.add(readableNetmask + "\n");
// Update 2     
mUpdates.add(readableIPAddress + "\n");
handler.post(new UpdateWiFiInfoTextViewRunnable());

where:

static public class UpdateWiFiInfoTextViewRunnable implements Runnable {
    public void run() {
        for (int i = 0; i < mUpdates.size(); i++) {
            wifi_info_textView.append(mUpdates.get(i));
        }
        mUpdates.clear();
    }
}

Upvotes: 1

Related Questions