Question

This is my .java file in src:

package com.wao.texttime;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TextTime extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView textView = (TextView) findViewById(R.id.TextView01);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());

        cal.set(Calendar.HOUR_OF_DAY, 6);
        cal.set(Calendar.MINUTE, 15);
        cal.set(Calendar.SECOND, 0);

        long vask_1 = cal.getTimeInMillis();

        cal.set(Calendar.HOUR_OF_DAY, 10);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        long vask_2 = cal.getTimeInMillis();

        cal.set(Calendar.HOUR_OF_DAY, 14);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        long vask_3 = cal.getTimeInMillis();

        cal.set(Calendar.HOUR_OF_DAY, 16);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        long vask_4 = cal.getTimeInMillis();

        cal.set(Calendar.HOUR_OF_DAY, 19);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        long vask_5 = cal.getTimeInMillis();
        long now = System.currentTimeMillis();

        if(now > vask_1 && now < vask_2)
        {
            textView.setText("5");
        }
        else if(now > vask_2 && now < vask_3)
        {
            textView.setText("1");
        }
        else if(now > vask_3 && now < vask_4)
        {
            textView.setText("2");
        }
        else if(now > vask_4 && now < vask_5)
        {
            textView.setText("3");

    }
    }
    }

I've been told I can use the OnResume to reload the current time to update the text in the TextView. But, I don't know how to use it in the file. Do you have any suggestions?

Was it helpful?

Solution

After the closing } of your onCreate method, add this:

@Override
protected void onResume(){
    super.onResume();
    // code to update the date here
}

You will also have to move the declarations of your TextView outside the onCreate method, or at least you will probably want to. To do this, add a line like this before the onCreate method:

private TextView textView;

and then in your onCreate you just need this:

textView = (TextView) findViewById(R.id.TextView01);

Then you can access textView from your onResume method too.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top