我想做这样的事情:
主要目标是显示从某些 Web 服务加载的微调器内容。
用户单击微调器 - 适配器应显示一些文本,例如“正在加载...”。当它最终被加载时 - 它应该用加载的内容替换微调器中的现有适配器。
我做了什么:我为它制作了一个微调器和onTouchListener。在 Listener 中,我正在启动asyncTask,它从 Web 服务下载数据。在asyncTask的onPostExecute中,我实际上是在尝试替换微调器适配器并执行spinner.performClick()。但事实上,用户同时看到了 2 个适配器 - 初始的一个(“正在加载...”)和新的一个,由spinner.performClick()显示。
有没有更好的想法如何完成初始任务或如何只显示一个适配器?
这是我使用的一些代码:
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;
}
});
}