gpt4 book ai didi

java - setText 在 while 循环中不起作用

转载 作者:行者123 更新时间:2023-11-29 08:30:42 25 4
gpt4 key购买 nike

我的应用程序从 API 获取流量更新(这有效)并返回一个 JSON 数组,然后我在 while 循环 (JSONobject) 中获取每个元素,并尝试每 5 秒用每个结果更新一个 TextView。

但是,我的脚本等待 15 秒,然后更新为最后一个值。我做了一些研究,它说要使用 asynctask,我已经这样做了,但没有什么不同。

我已经添加了 System.out.println(thestring_to_update_to),这就像我希望我的应用程序那样工作(每 5 秒更改一次)。

以下是在 try/catch block 中:

JSONArray TrafficInformation = new JSONArray(response);

int TrafficEvents = TrafficInformation.length();
int TrafficEvent = 0;

JSONObject CurrentEvent = new JSONObject();

do{
CurrentEvent = new JSONObject(TrafficInformation.getString(TrafficEvent));

TextView affected_route = (TextView)findViewById(R.id.disrupted_route);

try {
Object[] passTo = new Object[1];
passTo[0] = CurrentEvent.getString("9");

System.out.println(passTo[0]);

new tasker().doInBackground(passTo);

TrafficEvent++;
Thread.sleep(5000);
} catch (Exception e){
Toast.makeText(LiftShare.this, "There was an error with getting traffic info.", Toast.LENGTH_LONG).show();
}
} while (TrafficEvent < TrafficEvents);

我也有这个公开课

public class tasker extends AsyncTask {
@Override
protected Object[] doInBackground(Object[] Objects) {
TextView affected_route = (TextView)findViewById(R.id.disrupted_route);
affected_route.setText(Objects[0].toString());

return null;
};
}

这是进入代码的 JSONArray(格式正确)

Array
(
[0] => {"1":"Congestion","2":"Minor Disruption - up to 15 minutes delay","3":"Location : The M3 eastbound exit slip at junction J9 . \nReason : Congestion. \nStatus : Currently Active. \nReturn To Normal : Normal traffic conditions are expected between 11:30 and 11:45 on 25 January 2018. \nDelay : There are currently delays of 10 minutes against expected traffic. \n","7":"M3 J9 eastbound exit | Eastbound | Congestion","9":"M3","10":"South East","11":"Hampshire","14":"2018-01-25T11:22:38+00:00"}
[1] => {"1":"Overturned Vehicle","2":"Severe Disruption - in excess of 3 hours delay or road closure","3":"Location : The M3 westbound between junctions J8 and J9 . \nReason : Clearing the scene of an overturned vehicle. \nStatus : Currently Active. \nTime To Clear : The event is expected to clear between 14:45 and 15:00 on 25 January 2018. \nReturn To Normal : Normal traffic conditions are expected between 14:45 and 15:00 on 25 January 2018. \nLanes Closed : All lanes are closed. \nPrevious Reason : Following an earlier accident. \n","7":"M3 westbound between J8 and J9 | Westbound | Overturned Vehicle","9":"M3","10":"South East","11":"Hampshire","14":"2018-01-25T06:51:12+00:00"}
[2] => {"1":"Congestion","2":"Moderate Disruption - between 15 minutes and 3 hours delay","3":"Location : The A34 southbound between the A272 and the junction with the M3 . \nReason : Congestion. \nStatus : Currently Active. \nReturn To Normal : Normal traffic conditions are expected between 12:45 and 13:00 on 25 January 2018. \nDelay : There are currently delays of 40 minutes against expected traffic. \n","7":"A34 southbound within the A272 junction | Southbound | Congestion","9":"A34","10":"South East","11":"Hampshire","14":"2018-01-25T07:48:23+00:00"}
)

如何让 textview 每 5 秒更新一次新值?

最佳答案

你必须使用

new tasker().execute(passTo);

asynctask 作为一个线程启动,否则,在当前的实现中,它只会充当一个普通的方法调用

注意:您不能从后台线程更新 UI,即在 doInBackground 中,而是覆盖在 UI 线程上运行的 onPostExecute

@Override
protected Object[] doInBackground(Object[] Objects) {
TextView affected_route = (TextView)findViewById(R.id.disrupted_route);
//affected_route.setText(Objects[0].toString()); crash, instead do this in onPostExecute

return null;
};

更新:您可以使用 postDelayed 延迟一段时间后更新 UI

int i = 0;
affected_route.postDelayed(new Runnable() {
public void run() {
textView.setText(yourText);
}
},i+=5000);

关于java - setText 在 while 循环中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com