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

  • Home
  • SEARCH
  • 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 7642477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:11:58+00:00 2026-05-31T09:11:58+00:00

I want to update the UI from a timer. That’s not a problem at

  • 0

I want to update the UI from a timer. That’s not a problem at all but when it comes to Gallery/ListViews it gets difficult. I have a Gallery with custom BaseAdapter. I need a counter for every (gallery) item (every item counts different depending on the items data). That counter should run outside of the main thread. In addition I don’t want to run 10 threads for 10 items of the gallery when just one item is visible. It’s not a problem to define a Handler and start a thread (the counting) in the adapters getView()-method when a item/view gets visible. I can think of something like the following code:


public class MyAdapter extends BaseAdapter {

    static class ViewHolder {
         //...
         public Handler myHandler;
     }

     public View getView(int position, View convertView, ViewGroup parent) {

            //...
            // getView() gets called indefinite so first remove callback because it may be added already        
            holder.myHandler.removeCallbacks(myRunnable);
            holder.myHandler.postDelayed(myRunnable, 0);

            //...
     }

}

The problem is to remove the callback for a view thats not visible anymore because in getView() I get noticed when a view becomes visible but I have no clue how to get the view (and thereby the holder and it’s handler) that became invisible to remove the callback.

Is there a(nother) approach to solve that?

  • 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-31T09:12:00+00:00Added an answer on May 31, 2026 at 9:12 am

    Found a clean solution (if someone needs something like that). I needed to update a TextView to set the new counter value (every second).

    In BaseAdapters getView I add the TextViews(s) to a WeakHashMap. The TextView is the key of the map. A key/value mapping will be removed if the key is no longer referenced. So I do not cause memory leaks. The GarbageCollector does the work.

    A thread counts “counter objects” down and refreshes the TextViews with the corresponding values (GarbageCollector runs all the time so in my case the map consists mostly of 3 items because the gallery shows just one item. Due to GarbageCollection the map gets immediately cleared by “unused” TextViews specially when scrolling fast through the list/gallery)

    BaseAdapter’s getView():

    private WeakHashMap<TextView, Integer> counterMap = new WeakHashMap<TextView,Integer>();
    private Handler counterHandler = new Handler();
    
    public View getView(...) {
        //...
        counterMap.put(holder.tvCounter, position);
        //...
    }
    

    The counter:

    // Thread decrementing the time of the counter objects and updating the UI
    private final Runnable counterTimeTask = new Runnable() {
        public void run() {
            //...
            // decrement the remaining time of all objects
            for (CounterData data : counterDataList) 
                data.decrementTimeLeftInSeconds();
    
            // iterate through the map and update the containing TextViews
            for (Map.Entry<TextView, Integer> entry : counterMap.entrySet()) {
                TextView tvCounter = entry.getKey();
                Integer position = entry.getValue();
                CounterData data = counterDataList.get(position);
                long timeLeftInSeconds = data.getTimeLeftInSeconds();
                tvCounter.setText(" " + timeLeftInSeconds);
            }
    
            if (yourCondition)
                counterHandler.postDelayed(this, 1000);
        }
    };
    

    Start/stop the counter:

    public void startCounter() { counterHandler.post(counterTimeTask); }
    public void stopCounter() { counterHandler.removeCallbacks(counterTimeTask); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to update my sqlite database but i cannot find the way to
On my application I have a timer that checks for configuration updates from the
I've some database that i update with a service, and i want to update
I have a timer in my class that is I only ever want one
Possible Duplicate: How to update GUI from another thread in C#? My Timer event
I want update a column by adding days to current time. In pseudosyntax it
I want to update a list of storage devices as the user inserts USB
I want Windows Update to automatically download and install updates on my Vista machine,
I want to update two columns in a table. The value of the second
I want to update/upgrade the standard Leopard install of Sqlite3 to >3.5 to use

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.