Android:显示web服务内容的微调器
我想做这样的事情: 主要目标是显示从某些web服务加载的微调器内容 用户单击微调器-适配器应显示一些文本,如“加载…”。最后加载时,它应该用加载的内容替换spinner中现有的适配器 我做了什么:我为它做了一个旋转器和ontouch侦听器。在Listener中,我启动了asyncTask,它从web服务下载数据。在asyncTask的onPostExecute中,我正在尝试替换微调器适配器,并执行spinner.performClick()。但事实上,用户同时看到了两个适配器—初始适配器(“加载…”)和新适配器,如spinner.performClick()所示 有没有更好的方法来完成初始任务,或者如何只显示一个适配器 以下是我使用的一些代码:Android:显示web服务内容的微调器,android,user-interface,android-asynctask,spinner,Android,User Interface,Android Asynctask,Spinner,我想做这样的事情: 主要目标是显示从某些web服务加载的微调器内容 用户单击微调器-适配器应显示一些文本,如“加载…”。最后加载时,它应该用加载的内容替换spinner中现有的适配器 我做了什么:我为它做了一个旋转器和ontouch侦听器。在Listener中,我启动了asyncTask,它从web服务下载数据。在asyncTask的onPostExecute中,我正在尝试替换微调器适配器,并执行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;
}
});
}
私有类CountryRegionCity扩展异步任务{
受保护的JSONArray doInBackground(字符串…参数){
//从web服务获取内容
{...}
返回某个数组;
}
受保护的void onPostExecute(JSONArray JSONArray){
ArrayList countryNames=新的ArrayList();
//根据jsonArray在此处填写国家名称
{...}
ArrayAdapter=新的ArrayAdapter(上下文,android.R.layout.simple\u微调器\u项,国家名称);
mCountrySpinner.setAdapter(适配器);
mCountrySpinner.performClick();
}
}
公共视图onCreateDialogView(){
ArrayAdapter countryArrayAdapter=新的ArrayAdapter(context(),android.R.layout.simple_spinner_项,新字符串[]{“加载…”);
设置适配器(countryArrayAdapter);
mCountrySpinner.setOnTouchListener(新的OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
CountryRegionCity getCountries=新CountryRegionCity();
getCountries.执行(“国家”);
返回false;
}
});
}
我想您可以使用微调器控件的notifyDataSetChanged()
方法来显示适配器数据的更改。我想您可以使用微调器控件的notifyDataSetChanged()
方法来显示适配器数据的更改