0

I have one activity with two spinners which fills from database. Second spinner value is dependant on selected value of 1st spinner. I have following Code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.calculation_options);
    setProgressBarIndeterminateVisibility(true);
    setProgressBarVisibility(true);

    progressCalcualtionOptions = new ProgressDialog(this);

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onResume();
    try {
        progressCalcualtionOptions = ProgressDialog.show(
                context,
                Constant.lblPleasewait,
                Constant.lblPleasewait,
                true
        );

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //fill first spinner
                    list1 = db.GetAll(); //get from db

                    //updating ui from no ui thread
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            adapter1 = new Adapter(this,
                                    android.R.layout.simple_spinner_item,
                                    list1);
                            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);
                            spinner1.setAdapter(adapter1);
                        }
                    });

                    //Second first spinner
                    list2 = db.GetAll2(spinner1.getSelectedItemPosition); //get from db

                    //updating ui from no ui thread
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            adapter2 = new Adapter(this,
                                    android.R.layout.simple_spinner_item,
                                    list2);
                            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_item);

                            adapter2.setAdapter(ddldescriptionAdapter);
                        }
                    });


                } catch (Exception e) {
                }
                if (progressCalcualtionOptions != null) {
                    progressCalcualtionOptions.dismiss();
                    progressCalcualtionOptions = null;
                }
            }
        }).start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

In above code, I have used custom adapter to fill spinner and two database function for getting records from database.

Now the problem is that:

My second spinner is using selected item of spinner1 and I am updating spinner1 by using handler.post() which will execute later, so I will not able to get proper selected value of spinner1. it will give me -1 value of spinner1 because currently spinner1 will not updated on UI in mean time my code for spinner2 filling will executed.

How can I immediately update UI from non UI thread or how can I manage these thing?

Philar
  • 3,887
  • 1
  • 24
  • 19
Sushant Bhatnagar
  • 3,684
  • 10
  • 31
  • 42
  • Just put second spinner loading into new thread/async task inside on seleted change enent of first one – Selvin May 05 '14 at 19:05

1 Answers1

-1

You can use runOnUiThread

runOnUiThread:Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

Edit:

runOnUiThread(new Runnable() {

       adapter1= new Adapter(this,
                android.R.layout.simple_spinner_item,
                list1 );
      adapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);

    spinner1.setAdapter(adapter1);  

    });
Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
  • but how can i achieve this "Pause my current thread and execute UI thread then resume my current thread" , so that i will get proper value for spinner2 (means Selected Value of spinner1 ). plz give me some sample code or useful links . – Sushant Bhatnagar May 05 '14 at 13:23
  • http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.4_r1/android/app/Activity.java#Activity.runOnUiThread%28java.lang.Runnable%29 ... it is exactly the same what is he doing now ... – Selvin May 05 '14 at 13:59
  • 1
    @Selvin Activity.runOnUiThread() is a special case of more generic Handlers.To create Handler that is guaranteed to bind to UI (main) thread you should create Handler object binded to Main Looper like this: Handler mHandler = new Handler(Looper.getMainLooper());Check this link http://stackoverflow.com/questions/12618038/why-to-use-handlers-while-runonuithread-does-the-same – Zohra Khan May 05 '14 at 14:52
  • and? check the first comment under the question from your link ... it is just shortcut for handler ... so your answer has no added value - it will not change anything – Selvin May 05 '14 at 14:58
  • 1
    Before answering this question I looked into all aspects. In the accepted answer its written that Using Handlers instantiated with default constructor doesn't mean "code will run on UI thread" in general. By default, handlers binded to Thread from which they was instantiated from. – Zohra Khan May 05 '14 at 15:09