所以我在我的android应用程序中有这些代码行,wifiScrollViewText
是String类型,我设置为我想通过处理程序添加到ViewText的任何消息:wifiScrollViewText
...在我的情况下,readableNetmask
是255.255.255.0,readableIPAddress
是10.0.0.11 ...如果我删除了更新2,则网络掩码将出现在textview ...但如果我添加Update 2行代码,textview将显示IP两次而不是Netmask然后IPAddress。我认为解决方案是在启动第二个处理程序Object之前等待第一次更新完成!
// 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);
}
}
答案 0 :(得分:1)
在主线程上的当前消息/代码执行完毕之前,两个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();
}
}