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 8401049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:42:23+00:00 2026-06-09T21:42:23+00:00

I have an adapter and a spinner view which is set to use the

  • 0

I have an adapter and a spinner view which is set to use the adapter for it’s entries. I’m adding items to adapter from a list of all files in /assets/ folder, I found that this task takes very long time (even about 2 seconds for a list of 2 files on a 1.5Ghz phone!). Then I came up to use a worker thread to gather my list and not block UI thread. here is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    adapter = new ArrayAdapter<String>(SettingsActivity.this, android.R.layout.simple_spinner_item, fontsName);       
    Spinner fontsSpinner = (Spinner) findViewById(R.id.settings_font_spinner);
    fontsSpinner.setAdapter(adapter);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);         

    // The thread to gather font names from /assets/fonts/
    thread =  new Thread(){  
        @Override  
        public void run(){  
            try {
                String[] fileList = getAssets().list("fonts");
                if (fileList != null)
                    for (int i=0; i<fileList.length; i++) {
                        adapter.add(fileList[i]);               
                    }
            } catch (IOException e) {
            }                   
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                }
            });
        }  
    };        
    thread.start(); 

}

but it causes an error and crash:

08-17 13:54:01.017: E/AndroidRuntime(17929): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Am I using runOnUiThread() correctly?! It’s interesting that this code works perfectly when not using any thread, but it blocks UI (which is a pain).

any help on this please?

  • 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-06-09T21:42:24+00:00Added an answer on June 9, 2026 at 9:42 pm

    As zapl mentioned, adapter.add(Object) has to be called from the UI thread.

    You should use Android’s AsyncTask class rather than a Thread object. You could, for instance, load the data from file in AsyncTask’s doInBackground(Params...) method and update the list view in the onPostExecute(Result) method.

    Furthermore, you don’t need to call notifyDataSetChanged() explicitly; calls to adapter.add(Object) will notify the ListView that the adapter has changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have cursor which selects records from my CarProfile table and use custom adapter
i have an array which is populated in the spinner adapter. now i wanna
I have a spinner that show my data list from a database SQLite.. I
I have a ListFragment populated from a Cursor. I have the list background set
I have one spinner in which few values are there from strings.xml and I
I have spinner which I populate from SQLite database. But I want to populate
I have an activity which shows a Spinner (selection of category of items), a
I want to hide spinner form view i.e. set height to zero. (I have
I have a spinner with a custom adapter that I used to set the
I have a spinner getting views from a custom base adapter. The problem is

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.