Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7490829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:46:28+00:00 2026-05-29T15:46:28+00:00

I have a Timer in my onCreate(), it should set a new text when

  • 0

I have a Timer in my onCreate(), it should set a new text when value when the timeString changes. Unfortunately right now it doesn’t change at all.

Here is a snippet of my code. I don’t know what I’ve missed? If you guys have any suggestion then please let me know.

ViewPagerActivity

public class ViewPagerActivity extends MapActivity {
    private ViewPagerAdapter adapter;
    private ViewPager pager;
    private String timeString;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        adapter = new ViewPagerAdapter(this);
        pager = (ViewPager) findViewById(R.id.viewpager);
        TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
        pager.setAdapter(adapter);
        indicator.setViewPager(pager);

        Timer myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
                public void run() {
                    timerMethod();
                }
            }, 0, 1000);
    }

    private void timerMethod() {
        this.runOnUiThread(doTimer);
    }

    private Runnable doTimer = new Runnable() {
            private int totalSecs;
            private int hours;
            private int minutes;
            private int seconds;

            public void run() {

                totalSecs += 1;
                hours = totalSecs / 3600;
                minutes = (totalSecs % 3600) / 60;
                seconds = totalSecs % 60;

                timeString = (String) (hours > 9 ? String.valueOf(hours): AddZero(hours)) + ":" +
                    (String) (minutes > 9 ? String.valueOf(minutes): AddZero(minutes)) + ":"
                    +  (String) (seconds > 9 ? String.valueOf(seconds): AddZero(seconds));

                //pager.getAdapter().notifyDataSetChanged();

            }

            private String AddZero(int text) {
                return "0"+ text;
            }
        };
}

ViewPagerAdapter

public class ViewPagerAdapter extends PagerAdapter implements TitleProvider {
    private final Context context;
    private MapView mapView;
    private MapController mapController;
    private View view;

    public ViewPagerAdapter(Context context) {
            this.context = context;
    }

    private static String[] titles = new String[] { "Page1", "Map" };

    @Override
    public Object instantiateItem(View pager, final int position) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (position == 0) {
            view = inflater.inflate(R.layout.whereami, null);
            ((ViewPager) pager).addView(view, 0);
        //here, set text at first time when view was create only
        //and not update,when value of timeString change
        txtDurationValue = (TextView)view.findViewById(R.id.txtDurationValue);
        txtDurationValue.setText(timeString);

        }
        if (position == 1) {
            view = inflater.inflate(R.layout.my_map_activity, null);
            ((ViewPager) pager).addView(view, 0);
            mapView = (MapView) view.findViewById(R.id.mapview);

            mapController = mapView.getController();
            mapController.setZoom(14);
        }

        return view;
    }

 public int getItemPosition(Object object) {
     return POSITION_NONE;
 }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:46:29+00:00Added an answer on May 29, 2026 at 3:46 pm

    You have this line:

    txtDurationValue = (TextView)view.findViewById(R.id.txtDurationValue);
    

    I’m assuming you have a TextView txtDurationValue; in your adapter which you haven’t included in your question source code? If it isn’t in there add this inside the adapter. (Technically you shouldn’t expose raw values/views from the adapter, you should use get/sets but this should work until you refactor):

    public TextView txtDurationValue;
    

    You should then be able add

    public void setText(String text) {
        txtDurationValue.setText(text);
    }
    

    Which will mean you can change this //pager.getAdapter().notifyDataSetChanged(); to:

    ((ViewPagerAdapter)pager.getAdapter()).setText(text);
    

    What it doesn’t do is take care of the fact that you’re setting off a timer in onCreate() which is getting fired before the elements have been instantiated.

    This is my favourite Android hack (you can use indicator or any other view which you have reference to. Doesn’t really matter in this instance). It fires code directly after the view has been displayed:

        ViewTreeObserver vto = indicator .getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        myTimer.schedule(new TimerTask() {
                            public void run() {
                               timerMethod();
                            }
                        }, 0, 1000);
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array an a timer that adds a new object to my
I have three 2 tab TabA and TabB. TabA displays value its ok. Now
I have a timer that needs to not process its elapsed event handler at
I have a timer on a page in ASP.NET. After a certain period of
I have a timer in my JavaScript which needs to emulate clicking a link
I have this timer function, it gives me following exception. Collection was modified; enumeration
I have a timer that ticks every 3 seconds. If the timer found something
I have a control application - using asp.net webservices. I have a timer which
Can someone please point me to the easiest way to have a timer in
Hi all I have a BAckground worker and a Datatable. I have a timer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.