进度对话框未显示

发布于 2025-01-01 23:10:55 字数 7041 浏览 1 评论 0原文

我有两个旋转器,一个名为美食,一个名为其他区域。我正在以“init”作为参数调用新的 Thetask().execute 活动加载。在此基础上,应该通过网络服务设置旋转器,并提供可用的菜肴和区域列表。(此处不包括其方法)。最初,旋转器将选择“全部”,因此列表视图“餐厅列表”应该包含所有餐厅。当用户通过旋转器更改菜肴或区域时,餐厅列表列表视图应更新并显示进度对话框。 现在我的问题是所有代码都在工作,但是在填充列表视图后进度对话框开始显示,并且进度对话框永远不会停止,直到按下后退按钮。任何解决方案都将是 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);
      } 

}

I have two spinners one named cuisine and other area. I m calling the new Thetask().execute on activity load with "init" as argument. And on that the spinners should be set from webservice with the cuisines available and arealist.(methods for that not included here). And initially the spinners will have "All " selected so the listview"restaurantlist" should have all the restaurants. And when the user changes the cuisine or the area from spinners, the restaurantlist listview should be updated with a progressdialog been displayed.
Now My problem is that all the code is working but the progress dialog starts to display after the listview is populated and that progressdialog never stops until back button is pressed.Any solution for this will be a gr8 help.
Thanks in advance.

public class Thetask extends 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

}
}

//The code of getCuisines() is below as requested by you all. C if that can help

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);
      } 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2025-01-08 23:10:55

在 postexecute 中尝试一下

 if(pg.isShowing())
{
pg.dismiss();
}

,什么是 getCuisines();?
你那里有进度对话框吗?

如果它是嵌套的活动,那么尝试

  ProgressDialog dialogGoog = new ProgressDialog(getParent());

try this in postexecute

 if(pg.isShowing())
{
pg.dismiss();
}

and What is getCuisines();?
do you have any ProgressDialog over there?

if its nested activity then try

  ProgressDialog dialogGoog = new ProgressDialog(getParent());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文