I’ve an activity, in which I am using Spinner to populate it from WebService call, with the help of AsyncTask. My problem is, in logcat, I can see all the ArrayAdapter<String> items, but when I give this adapter to my Spinner, its not displaying the drop-down list items.
Here is my code snippet:
public static ArrayAdapter<String> adapter;
Spinner spnrCourseType;
spnrCourseType.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
new DownloadCourseTypesList().execute(training_id);
return true;
}
});
spnrCourseType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ting("You Selected : " + adapter.getItem(arg2));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
/** Thread AsyncTask Class */
private class DownloadCourseTypesList extends AsyncTask<String,Void,ArrayList<String>> {
ProgressDialog progressDialog = new ProgressDialog(CorporateTrainingInquiryActivity.this);
protected void onPreExecute(){
actionBar.setProgressBarVisibility(View.VISIBLE);
progressDialog.setTitle("Course Types Loading..");
progressDialog.setMessage("Please Wait..");
progressDialog.show();
}
protected ArrayList<String> doInBackground(String... params) {
trainingList = new TrainingList(params[0]);
ArrayList<String> values = trainingList.getTrainingList();
// adapter = new ArrayAdapter<String>(CorporateTrainingInquiryActivity.this,android.R.layout.simple_spinner_item,values);
return values;
}
protected void onPostExecute(ArrayList<String> values) {
actionBar.setProgressBarVisibility(View.GONE);
adapter = new ArrayAdapter<String>(CorporateTrainingInquiryActivity.this,android.R.layout.simple_spinner_item,values);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.insert("Select Course Type",0);
spnrCourseType.setAdapter(adapter);
for(int i=0;i<adapter.getCount();i++){
System.out.println("Item in adapter at position : " + i + adapter.getItem(i));
}
adapter.notifyDataSetChanged();
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
}
}
When I execute the above code, the ProgressDialog gets visible, and only first item, i.e “Select Course Type” is getting visible on Spinner itself, but not in a drop-down list items. Any Clues about it. I am stuck in this from the yesterday itself 🙁

This code:-
Could be causing some kind of race condition, when you press the spinner, your task is executed, but it might not be able to populate the spinner quick enough for you to see the result.
I would recommend moving this code elsewhere, like an onCreate method, or equivalent.
Hope that helps.