Reputation: 4575
I want to make such thing:
The main goal is to display spinner contents loaded from some web service.
User clicks on spinner - adapter should show some text, like "loading...". And when it finally be loaded - it should replace existing adapter in spinner with loaded contents.
What did I do: I've made a spinner and onTouchListener for it. In Listener I am launching asyncTask, which downloads data from web service. In onPostExecute of asyncTask I am trying actually to replace spinners adapter and do spinner.performClick(). But in fact, user is seeing 2 adapters simultaneously - initial one ("Loading...") and the new one, which was shown by spinner.performClick().
Are there any better ideas how can be done initial task or how I can show only one adapter?
Here is some code I use:
private class CountryRegionCity extends AsyncTask<String, Void, JSONArray> {
protected JSONArray doInBackground(String... params) {
// Getting contents from web service
{...}
return someJSONArray;
}
protected void onPostExecute (JSONArray jsonArray) {
ArrayList<String> countryNames = new ArrayList<String>();
//Filling here countryNames based on jsonArray
{...}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, countryNames);
mCountrySpinner.setAdapter(adapter);
mCountrySpinner.performClick();
}
}
public View onCreateDialogView() {
ArrayAdapter<String> countryArrayAdapter=new ArrayAdapter<String> (context(), android.R.layout.simple_spinner_item, new String[] {"Loading..."});
mCountrySpinner.setAdapter(countryArrayAdapter);
mCountrySpinner.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
CountryRegionCity getCountries = new CountryRegionCity();
getCountries.execute("countries");
return false;
}
});
}
Upvotes: 1
Views: 1047
Reputation: 362
I guess you can user notifyDataSetChanged()
method of Spinner control to show the change in adapter data.
Upvotes: 1