setText无法在while循环中工作

时间:2018-01-25 16:23:38

标签: java android android-layout

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

但是,我的脚本等待15秒,然后更新到最后一个值。我已经完成了一些研究,它说要使用asynctask,我已经做过,但它没有什么区别。

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

以下是在try / catch块中:

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秒更新一次新值?

2 个答案:

答案 0 :(得分:3)

你必须使用

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延迟一段时间后更新用户界面

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

答案 1 :(得分:1)

AsyncTask似乎对你的要求有些过分,因为你并没有真正在后台做任何工作。您可以使用Handler(来自android.os)安排在一段时间后更新文字,如下所示:

Handler handler = new Handler(Looper.getMainLooper());
    Runnable textUpdater = new Runnable() {
        @Override
        public void run() {
            // this needs to execute in the UI thread
            affected_route.setText(lastUpdate);
        }
    };
    String lastUpdate = "Store your last update here";

    void updateText(){
        handler.postDelayed(textUpdater, 5000);
    }