【发布时间】:2012-02-02 11:41:30
【问题描述】:
我有两个微调器,一个命名为美食,另一个命名为其他区域。我在以“init”作为参数的活动加载时调用新的 Thetask().execute。并且应该从 web 服务中设置微调器,其中包含可用的美食和 arealist。(此处未包括的方法)。最初,微调器将选择“All”,因此列表视图“restaurantlist”应该包含所有餐厅。并且当用户从微调器更改美食或区域时,应该使用显示的进度对话框更新餐厅列表列表视图。 现在我的问题是所有代码都在工作,但是在填充列表视图后进度对话框开始显示,并且进度对话框在按下后退按钮之前永远不会停止。任何解决方案都将是 gr8 帮助。 提前致谢。
公共类 Thetask 扩展 AsyncTask {
String flagdopost="",x,y;
@Override
public void onPreExecute()
{
pg =new ProgressDialog(Restaurantlistdisplay.this);
pg.setMessage("fetching info....");
pg.setIndeterminate(true);
pg.setCancelable(true);
Log.i("inside preexecute","in pre execute");
pg.show();
}
public Void doInBackground(String... params)
{
if(params[0].equalsIgnoreCase("init"))
{
tempcuisinenamelist = getCuisines();
tempcuisinenamelist.add(0,"All");
tempareanamelist=getAreanames(cid, sid, cityid);
tempareanamelist.add(0,"All");
flagdopost="init";
Log.i("inside doinbackground 1st",flagdopost);
}
else if(params[0].equalsIgnoreCase(""))
{
firequery();
flagdopost="itemselected";
Log.i("inside doinbackground 2nd",flagdopost);
}
else if(params[0].equalsIgnoreCase("itemclick"))
{
//sendid=getRestaurantId(params[1],params[2]);
x=params[1];
y=params[2];
Log.i("in do in backgroung idforbundle is", Integer.toString(sendid));
flagdopost="itemclicked";
}
return null;
}
public void onPostExecute(Void result)
{
if(flagdopost.equalsIgnoreCase("init"))
{
ArrayAdapter<String> adaptercuisine=new ArrayAdapter<String> (Restaurantlistdisplay.this,android.R.layout.simple_spinner_item,tempcuisinenamelist);
adaptercuisine.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapterarea=new ArrayAdapter<String>(Restaurantlistdisplay.this,android.R.layout.simple_spinner_item,tempareanamelist);
adapterarea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
area.setAdapter(adapterarea);
area.setSelection(0);
area.setOnItemSelectedListener(Restaurantlistdisplay.this);
cuisine.setAdapter(adaptercuisine);
cuisine.setSelection(0);
cuisine.setOnItemSelectedListener(Restaurantlistdisplay.this);
}
else if(flagdopost.equalsIgnoreCase("itemselected"))
{
getResult(str);
customlist.clear();
populateReslist();
//Log.i("inside 1st firequery","list populated"+customlist.toString());
restaurant.invalidateViews();
restaurant.setAdapter(new SimpleAdapter(Restaurantlistdisplay.this,customlist,R.layout.customlistrow,new String[] {"Restaurant Name","Area Name"},
new int[] {R.id.tvresname,R.id.tvareaname})
{
@Override
public View getView(int position, View convertView,ViewGroup parent)
{
View view =super.getView(position, convertView, parent);
return view;
}
});
}
else if(flagdopost.equalsIgnoreCase("itemclicked"))
{
sendid=getRestaurantId(x,y);
Bundle bundle=new Bundle();
bundle.putInt("locid",sendid);
Toast.makeText(getBaseContext(), "locId in dopost new one"+sendid, 10).show();
Intent resdetail = new Intent(Restaurantlistdisplay.this,Restaurantdetail.class);
resdetail.putExtras(bundle);
startActivity(resdetail);
}
pg.dismiss();
}
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
switch(arg0.getId())
{
case R.id.spncuisine: asynctaskflag="";
if(cuisine.getSelectedItem().toString()!="All")
{
cuisineval=cuisine.getSelectedItem().toString();
}
else
{
cuisineval="*";
}
new Thetask().execute(asynctaskflag,null,null);
break;
case R.id.spnarea:
asynctaskflag="";
if(area.getSelectedItem().toString()!="All")
{
areaval=area.getSelectedItem().toString();
}
else
{
areaval="*";
}
new Thetask().execute(asynctaskflag,null,null);
break;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
//getCuisines()的代码如下,根据大家的要求。 C 如果有帮助的话
public List<String> getCuisines()
{
String list = null;
cuisinelist=new ArrayList<String>();
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("str", //here goes query to fetch data from table of </br>cuisines);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
aht.debug = true;
aht.call(SOAP_ACTION, envelope);
SoapPrimitive results = (SoapPrimitive)envelope.getResponse();
list=results.toString();
}
catch (SocketException ex)
{
ex.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(new InputSource(new StringReader(list)));
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("row");
for (int i=0;i<items.getLength();i++)
{
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j=0;j<properties.getLength();j++)
{
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("typeName"))
{
cuisinelist.add(property.getFirstChild().getNodeValue());
}
}
}
return cuisinelist;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
【问题讨论】:
-
getCuisines() 的邮政编码。
-
@Akki 发布了 getcuisines 的代码。请看看。但是伙计,我认为当控件在人口美食的微调器之后立即转到微调器的 itemselected 时,新的 Task().execute() 会再次运行,因此可能需要再次加载进度对话框。