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?