正在更新接口处理程序问题



所以我在我的android应用程序中有这些行代码,wifiScrollViewText是类型字符串,我设置为我想要附加到ViewText的任何消息:wifiScrollViewText通过处理程序…在本例中,readableNetmask为255.255.255.0,readableIPAddress为10.0.0.11…如果我删除更新2的Netmask将出现在textview…但如果我添加更新2行代码,textview将显示IP两次,而不是Netmask,然后是IPAddress。我认为解决方案是在开始第二个处理程序对象之前等待第一次更新完成!

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

可运行:

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

两个Runnables将不会运行,直到主线程上的当前消息/代码完成执行,所以当两个Runnables运行时,wifiScrollViewText变量指向相同的文本。您需要将这两段文本保存在两个单独的变量或列表中(如果您计划进行多次追加),并在Runnable:

的单次运行中弹出它们。
List<String> mUpdates = new ArrayList<String>();
// Update 1 
mUpdates.add(readableNetmask + "n");
// Update 2     
mUpdates.add(readableIPAddress + "n");
handler.post(new UpdateWiFiInfoTextViewRunnable());

地点:

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

最新更新