Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 23.22 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. progress dialog keeps on displaying
  2. package com.Refinedymapps;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.StringReader;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12.  
  13. import javax.xml.parsers.DocumentBuilder;
  14. import javax.xml.parsers.DocumentBuilderFactory;
  15.  
  16. import org.apache.http.HttpEntity;
  17. import org.apache.http.HttpResponse;
  18. import org.apache.http.client.HttpClient;
  19. import org.apache.http.client.methods.HttpGet;
  20. import org.apache.http.impl.client.DefaultHttpClient;
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Element;
  23. import org.w3c.dom.Node;
  24. import org.w3c.dom.NodeList;
  25. import org.xml.sax.InputSource;
  26.  
  27. import android.app.Activity;
  28. import android.app.ProgressDialog;
  29. import android.content.Intent;
  30. import android.location.Geocoder;
  31. import android.location.LocationManager;
  32. import android.os.AsyncTask;
  33. import android.os.Bundle;
  34. import android.util.Log;
  35. import android.view.View;
  36. import android.view.View.OnClickListener;
  37. import android.view.ViewGroup;
  38. import android.widget.AdapterView;
  39. import android.widget.AdapterView.OnItemClickListener;
  40. import android.widget.AdapterView.OnItemSelectedListener;
  41. import android.widget.ArrayAdapter;
  42. import android.widget.ImageButton;
  43. import android.widget.ListView;
  44. import android.widget.SimpleAdapter;
  45. import android.widget.Spinner;
  46. import android.widget.TextView;
  47. import android.widget.Toast;
  48.  
  49. public class Restaurantlist extends Activity implements OnClickListener, OnItemSelectedListener {
  50.     ProgressDialog pg;
  51.     private ImageButton imgbtn;
  52.     private ListView restaurant;
  53.     private TextView city;
  54.     String sendid,flagdopost="",linkurl;
  55.     LocationManager locationManger;
  56.     Geocoder gc;
  57.     Bundle bundle;
  58.     private int j=0;
  59.     private Spinner cuisine,area;
  60.     static ArrayList<HashMap<String,String>> customlist= new ArrayList<HashMap<String,String>>();
  61.     private String asynctaskflag="",cuisineval,areaval;
  62.     List<String> tempareanamelist,tempcuisinenamelist,areanamelist,cuisinelist,cuisineidlist,restaurantnames,areanames,restaurantidlist,originalcuisinelist,originalarealist;
  63.     String str,coun,stat,cid,sid,cityid,cityname;
  64.  
  65.  
  66.  
  67.      @Override
  68.     public void onCreate(Bundle savedInstanceState)
  69.      {
  70.             super.onCreate(savedInstanceState);
  71.             setContentView(R.layout.restaurantlisting);
  72.             linkurl=getResources().getString(R.string.linkurl);
  73.             city=(TextView)findViewById(R.id.textviewcity);
  74.             cuisine=(Spinner)findViewById(R.id.spncuisine);
  75.             area=(Spinner)findViewById(R.id.spnarea);
  76.             restaurant=(ListView)findViewById(R.id.lstrestaurant);
  77.             imgbtn=(ImageButton)findViewById(R.id.imageButton1);            
  78.  
  79.             bundle = this.getIntent().getExtras();
  80.             imgbtn.setImageResource(R.drawable.searchloc);
  81.             asynctaskflag="init";
  82.             new Thetask().execute(asynctaskflag,null,null);                
  83.  
  84.             imgbtn.setOnClickListener(new OnClickListener(){
  85.                 public void onClick(View arg0) {
  86.                     Intent newlocintent=new Intent(Restaurantlist.this,Restaurantlistdisplay.class);
  87.                     startActivity(newlocintent);
  88.  
  89.                     }              
  90.             });            
  91.             restaurant.setOnItemClickListener(new OnItemClickListener(){                
  92.                 public void onItemClick(AdapterView<?> arg0, View arg1,
  93.                         int arg2, long arg3) {
  94.                         Toast.makeText(getBaseContext(), "resname"+restaurantnames.get(arg2)+"areaname"+areanames.get(arg2), 10).show();
  95.                         String[] getid={"itemclick",restaurantnames.get(arg2),areanames.get(arg2)};
  96.                         new Thetask().execute(getid);                                      
  97.                 }
  98.             });
  99.      }
  100.  
  101.      public void populateReslist()
  102.      {
  103.         Log.i("inside populatereslist","value of j is:"+j);
  104.         for(int i=0;i<restaurantnames.size();i++)
  105.         {
  106.             HashMap<String,String> temp = new HashMap<String,String>();
  107.             temp.put("Restaurant Name",restaurantnames.get(i).toString());
  108.             temp.put("Area Name", areanames.get(i).toString());
  109.             customlist.add(temp);
  110.         }  
  111.      }
  112.  
  113.      public List<String> getAreanames(String cityid)
  114.         {
  115.  
  116.             String list = null;
  117.             areanamelist=new ArrayList<String>();
  118.  
  119.             final HttpClient client=new DefaultHttpClient();
  120.  
  121.             final HttpGet req=new HttpGet(linkurl+"/ymaws/resources/restaurant/"+cityid+"/areas/");
  122.             HttpResponse httpResponse;
  123.             try {
  124.                     httpResponse=client.execute(req);
  125.                     HttpEntity entity = httpResponse.getEntity();
  126.                     Log.i("entity", entity.toString());
  127.                     if (entity != null)
  128.                     {
  129.  
  130.                         InputStream instream = entity.getContent();
  131.                         BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
  132.                         StringBuilder sb = new StringBuilder();
  133.  
  134.                         String line = null;
  135.                         try
  136.                         {
  137.                             while ((line = reader.readLine()) != null)
  138.                             {
  139.                                 sb.append(line + "n");
  140.                             }
  141.                         }
  142.                         catch (IOException e)
  143.                         {
  144.                             e.printStackTrace();
  145.                         }
  146.                         finally
  147.                         {
  148.                             try
  149.                             {
  150.                                 instream.close();
  151.                             }
  152.                             catch (IOException e)
  153.                             {
  154.                                 e.printStackTrace();
  155.                             }
  156.                         }
  157.  
  158.                         // Closing the input stream will trigger connection release
  159.                         list= sb.toString();
  160.                         Log.i("areaname xml is", list.toString());
  161.                     }
  162.             }
  163.             catch(Exception e)
  164.             {
  165.  
  166.             }
  167.  
  168.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  169.             factory.setNamespaceAware(true);
  170.             try
  171.             {
  172.                 DocumentBuilder builder = factory.newDocumentBuilder();
  173.                 Document dom = builder.parse(new InputSource(new StringReader(list)));
  174.                 Element root = dom.getDocumentElement();
  175.                 NodeList items = root.getElementsByTagName("Area");
  176.                 for (int i=0;i<items.getLength();i++)
  177.                 {
  178.                     Node item = items.item(i);
  179.                     NodeList properties = item.getChildNodes();
  180.                     for (int j=0;j<properties.getLength();j++)
  181.                     {
  182.                         Node property = properties.item(j);
  183.                         String name = property.getNodeName();
  184.                         if (name.equalsIgnoreCase("areaName"))
  185.                         {
  186.                             areanamelist.add(property.getFirstChild().getNodeValue());
  187.                         }                      
  188.                         else
  189.                         {
  190.  
  191.                         }                  
  192.                     }
  193.                 }
  194.                 //Log.i("areaname list in getAreas method", areanamelist.toString());
  195.                 return areanamelist;        
  196.            }
  197.             catch (Exception e)
  198.             {
  199.                 throw new RuntimeException(e);
  200.             }
  201.         }
  202.  
  203.      public List<String> getCuisines(String cityid)
  204.      {
  205.  
  206.             String list = null;
  207.             cuisinelist=new ArrayList<String>();
  208.             cuisineidlist=new ArrayList<String>();
  209.             final HttpClient client=new DefaultHttpClient();            
  210.             final HttpGet req=new HttpGet(linkurl+"/ymaws/resources/restaurant/"+cityid+"/cuisines/");
  211.             HttpResponse httpResponse;
  212.             try {
  213.                     httpResponse=client.execute(req);
  214.                     HttpEntity entity = httpResponse.getEntity();
  215.                     Log.i("entity", entity.toString());
  216.                     if (entity != null)
  217.                     {
  218.  
  219.                         InputStream instream = entity.getContent();
  220.                         BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
  221.                         StringBuilder sb = new StringBuilder();
  222.  
  223.                         String line = null;
  224.                         try
  225.                         {
  226.                             while ((line = reader.readLine()) != null)
  227.                             {
  228.                                 sb.append(line + "n");
  229.                             }
  230.                         }
  231.                         catch (IOException e)
  232.                         {
  233.                             e.printStackTrace();
  234.                         }
  235.                         finally
  236.                         {
  237.                             try
  238.                             {
  239.                                 instream.close();
  240.                             }
  241.                             catch (IOException e)
  242.                             {
  243.                                 e.printStackTrace();
  244.                             }
  245.                         }
  246.  
  247.                         // Closing the input stream will trigger connection release
  248.                         list= sb.toString();
  249.                         Log.i("cuisinename xml is", list.toString());
  250.                     }
  251.             }
  252.             catch(Exception e)
  253.             {
  254.  
  255.             }
  256.  
  257.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  258.             factory.setNamespaceAware(true);
  259.             try
  260.             {
  261.                 DocumentBuilder builder = factory.newDocumentBuilder();
  262.                 Document dom = builder.parse(new InputSource(new StringReader(list)));
  263.                 Element root = dom.getDocumentElement();
  264.                 NodeList items = root.getElementsByTagName("Cuisine");
  265.                 for (int i=0;i<items.getLength();i++)
  266.                 {
  267.                     Node item = items.item(i);
  268.                     NodeList properties = item.getChildNodes();
  269.                     for (int j=0;j<properties.getLength();j++)
  270.                     {
  271.                         Node property = properties.item(j);
  272.                         String name = property.getNodeName();
  273.                         if (name.equalsIgnoreCase("cuisineName"))
  274.                         {
  275.                             cuisinelist.add(property.getFirstChild().getNodeValue());
  276.                         }                      
  277.                         else if(name.equalsIgnoreCase("cuisineId"))
  278.                         {
  279.                             cuisineidlist.add(property.getFirstChild().getNodeValue());
  280.                         }                  
  281.                     }
  282.                 }  
  283.                 Log.i("getcuisine cuisinelist",cuisinelist.toString());
  284.                 return cuisinelist;        
  285.            }
  286.             catch (Exception e)
  287.             {
  288.                 throw new RuntimeException(e);
  289.             }
  290.  
  291.      }
  292.  
  293.      public void firequery()
  294.         {
  295.             String url = null;
  296.             if(cuisineval=="*" && areaval=="*")
  297.             {
  298.                  url=linkurl+"/ymaws/resources/restaurant/"+cityid+"/restaurants";                          
  299.             }
  300.             else if(cuisineval!="*" && areaval=="*")
  301.             {
  302.  
  303.                 for(int i=0;i<originalcuisinelist.size();i++)
  304.                 {  
  305.  
  306.                     Log.i("cuisinelist original item is",""+originalcuisinelist.get(i));
  307.                     Log.i("cuisineidlist is",cuisineidlist.toString());
  308.  
  309.                     if((cuisine.getSelectedItem().toString()).equalsIgnoreCase(originalcuisinelist.get(i).toString()))
  310.                     {                      
  311.                         Log.i("cuisineid selected is",""+cuisineidlist.get(i));
  312.                         url=linkurl+"/ymaws/resources/restaurant?cityid="+cityid+"&cuisineid="+cuisineidlist.get(i);
  313.                     }
  314.                 }      
  315.             }
  316.             else if(cuisineval=="*" && areaval!="*")
  317.             {
  318.  
  319.                             url = linkurl+"/ymaws/resources/restaurant?cityid="+cityid+"&areanm="+area.getSelectedItem().toString();
  320.  
  321.  
  322.             }
  323.             else
  324.             {
  325.                 for(int i=0;i<originalcuisinelist.size();i++)
  326.                 {              
  327.                     if((cuisine.getSelectedItem().toString()).equalsIgnoreCase(originalcuisinelist.get(i).toString()))
  328.                     {              
  329.                         url=linkurl+"/ymaws/resources/restaurant?cityid="+cityid+"&cuisineid="+cuisineidlist.get(i)+"&areanm="+area.getSelectedItem().toString();
  330.                     }
  331.                 }                  
  332.             }
  333.             getResult(url);
  334.  
  335.         }
  336.  
  337.      public void getResult(String url)
  338.      {
  339.             String list = null;
  340.             restaurantnames=new ArrayList<String> ();  
  341.             areanames=new ArrayList<String>();
  342.             restaurantidlist=new ArrayList<String>();
  343.                         final HttpClient client=new DefaultHttpClient();
  344.             Log.i("url is","url:"+url.toString());
  345.             final HttpGet req=new HttpGet(url.replaceAll(" ", "%20"));
  346.             HttpResponse httpResponse;
  347.             try {
  348.                     httpResponse=client.execute(req);
  349.                     HttpEntity entity = httpResponse.getEntity();
  350.                     Log.i("entity", entity.toString());
  351.                     if (entity != null)
  352.                     {
  353.  
  354.                         InputStream instream = entity.getContent();
  355.                         BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
  356.                         StringBuilder sb = new StringBuilder();
  357.                         String line = null;
  358.                         try
  359.                         {
  360.                             while ((line = reader.readLine()) != null)
  361.                             {
  362.                                 sb.append(line + "n");
  363.                             }
  364.                         }
  365.                         catch (IOException e)
  366.                         {
  367.                             e.printStackTrace();
  368.                         }
  369.                         finally
  370.                         {
  371.                             try
  372.                             {
  373.                                 instream.close();
  374.                             }
  375.                             catch (IOException e)
  376.                             {
  377.                                 e.printStackTrace();
  378.                             }
  379.                         }                      
  380.                         list= sb.toString();
  381.                         Log.i("list xml is", list.toString());
  382.                     }
  383.             }
  384.             catch(Exception e)
  385.             {
  386.  
  387.             }
  388.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  389.             factory.setNamespaceAware(true);
  390.             try
  391.             {
  392.               if(list!=null)
  393.               {
  394.                 DocumentBuilder builder = factory.newDocumentBuilder();
  395.                 Document dom = builder.parse(new InputSource(new StringReader(list)));
  396.                 Element root = dom.getDocumentElement();
  397.                 NodeList items = root.getElementsByTagName("resLovData");
  398.                 for (int i=0;i<items.getLength();i++)
  399.                 {
  400.                     Node item = items.item(i);
  401.                     NodeList properties = item.getChildNodes();
  402.                     for (int j=0;j<properties.getLength();j++)
  403.                     {
  404.                         Node property = properties.item(j);
  405.                         String name = property.getNodeName();
  406.                         if (name.equalsIgnoreCase("locName"))
  407.                         {
  408.                             restaurantnames.add(property.getFirstChild().getNodeValue());
  409.                         }                      
  410.                         else if(name.equalsIgnoreCase("areaName"))
  411.                         {
  412.                             areanames.add(property.getFirstChild().getNodeValue());
  413.                         }
  414.                         else if(name.equalsIgnoreCase("locId"))
  415.                         {
  416.                             restaurantidlist.add(property.getFirstChild().getNodeValue());
  417.                         }
  418.  
  419.                     }
  420.                 }  
  421.               }
  422.               else
  423.               {
  424.  
  425.               }
  426.            }
  427.             catch (Exception e)
  428.             {
  429.                 throw new RuntimeException(e);
  430.             }
  431.      }
  432.  
  433.      public void onClick(View arg0) {
  434.         // TODO Auto-generated method stub
  435.  
  436.     }
  437.  
  438.      public class Thetask extends AsyncTask<String, Void, Void>  
  439.      {    
  440.  
  441.          String x,y;        
  442.          @Override
  443.          public void onPreExecute()
  444.          {  
  445.             pg =new ProgressDialog(Restaurantlist.this);
  446.             pg.setMessage("fetching info....");
  447.             pg.setIndeterminate(true);
  448.             pg.setCancelable(true);
  449.             Log.i("inside preexecute","in pre execute");
  450.             pg.show();          
  451.          }  
  452.  
  453.          public Void doInBackground(String... params)
  454.          {          
  455.             if(params[0].equalsIgnoreCase("init"))
  456.             {
  457.                 Log.i("inside doinbackground 1st",flagdopost);
  458.                 cid=bundle.getString("cid");
  459.                 sid=bundle.getString("sid");
  460.                 cityid=bundle.getString("cityid");              
  461.                 cityname=bundle.getString("cityname");
  462.                 originalcuisinelist=new ArrayList<String>();
  463.                 originalcuisinelist=getCuisines(cityid);
  464.  
  465.                 tempcuisinenamelist = getCuisines(cityid);  
  466.                 tempcuisinenamelist.add(0,"All");
  467.  
  468.                 originalarealist=new ArrayList<String>();
  469.                 originalarealist=getAreanames(cityid);
  470.                 tempareanamelist=getAreanames(cityid);
  471.                 tempareanamelist.add(0,"All");
  472.                 Log.i("area are",tempareanamelist.toString());
  473.  
  474.                 flagdopost="init";  
  475.                 Log.i("inside doinbackground 1st",flagdopost);
  476.             }          
  477.             else if(params[0].equalsIgnoreCase(""))
  478.             {
  479.                 flagdopost="";
  480.                  j++;
  481.                  if(j!=1)
  482.                  {
  483.                      firequery();          
  484.                      flagdopost="itemselected";
  485.                      Log.i("inside doinbackground 2nd",flagdopost);
  486.                  }
  487.             }          
  488.             else if(params[0].equalsIgnoreCase("itemclick"))
  489.             {
  490.                 x=params[1];
  491.                 y=params[2];
  492.                 for(int i=0;i<restaurantidlist.size();i++)
  493.                 {
  494.                     if((params[1].toString()).equalsIgnoreCase(restaurantnames.get(i)))
  495.                     {
  496.                         sendid=restaurantidlist.get(i);
  497.  
  498.                     }
  499.                 }
  500.  
  501.                 Log.i("in do in backgroung idforbundle is", sendid);
  502.                 flagdopost="itemclicked";
  503.             }
  504.             return null;                    
  505.          }
  506.  
  507.          public void onPostExecute(Void result)
  508.          {      
  509.              if(flagdopost.equalsIgnoreCase("init"))
  510.              {
  511.                  city.setText(cityname);                            
  512.                  ArrayAdapter<String> adaptercuisine=new ArrayAdapter<String>(Restaurantlist.this,android.R.layout.simple_spinner_item,tempcuisinenamelist);
  513.                  adaptercuisine.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  514.  
  515.                  ArrayAdapter<String> adapterarea=new ArrayAdapter<String>(Restaurantlist.this,android.R.layout.simple_spinner_item,tempareanamelist);
  516.                  adapterarea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  517.  
  518.                  area.setAdapter(adapterarea);
  519.                  area.setSelection(0);
  520.  
  521.                  cuisine.setAdapter(adaptercuisine);
  522.                  cuisine.setSelection(0);
  523.  
  524.                  area.setOnItemSelectedListener(Restaurantlist.this);
  525.                  cuisine.setOnItemSelectedListener(Restaurantlist.this);
  526.              }
  527.              else if(flagdopost.equalsIgnoreCase("itemselected"))
  528.              {
  529.  
  530.                  customlist.clear();
  531.                  populateReslist();
  532.                  restaurant.invalidateViews();
  533.                  restaurant.setAdapter(new SimpleAdapter(Restaurantlist.this,customlist,R.layout.customlistrow,new String[] {"Restaurant Name","Area Name"},
  534.                  new int[] {R.id.tvresname,R.id.tvareaname})
  535.                  {
  536.                    @Override      
  537.                    public View getView(int position, View convertView,ViewGroup parent)
  538.                    {                                                  
  539.                        View view =super.getView(position, convertView, parent);
  540.                        return view;                    
  541.                    }  
  542.                  });    
  543.  
  544.              }
  545.              else if(flagdopost.equalsIgnoreCase("itemclicked"))
  546.              {              
  547.                 Bundle bundle=new Bundle();
  548.                 bundle.putString("locid",sendid);
  549.  
  550.                 Toast.makeText(getBaseContext(), "locId in dopost new one"+sendid, 10).show();
  551.                 Intent resdetail = new Intent(Restaurantlist.this,Restaurantdetail.class);
  552.                 resdetail.putExtras(bundle);
  553.                 startActivity(resdetail);              
  554.              }              
  555.              pg.dismiss();
  556.          }
  557.  
  558.   }
  559.  
  560.     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
  561.             long arg3)
  562.     {
  563.         switch(arg0.getId())
  564.         {
  565.             case    R.id.spncuisine:
  566.  
  567.                                     asynctaskflag="";
  568.                                     if(cuisine.getSelectedItem().toString()!="All")
  569.                                     {
  570.                                         cuisineval=cuisine.getSelectedItem().toString();
  571.                                     }
  572.                                     else
  573.                                     {
  574.                                         cuisineval="*";
  575.                                     }
  576.  
  577.                                     new Thetask().execute(asynctaskflag,null,null);
  578.  
  579.                                     break;
  580.  
  581.              case   R.id.spnarea:                          
  582.                                     asynctaskflag="";
  583.                                     if(area.getSelectedItem().toString()!="All")
  584.                                     {
  585.                                         areaval=area.getSelectedItem().toString();
  586.                                     }
  587.                                     else
  588.                                     {
  589.                                         areaval="*";
  590.                                     }
  591.                                     new Thetask().execute(asynctaskflag,null,null);
  592.                                     break;
  593.         }
  594.  
  595.  
  596.     }
  597.     public void onNothingSelected(AdapterView<?> arg0) {
  598.         // TODO Auto-generated method stub
  599.  
  600.     }
  601.  
  602. }
  603.        
  604. public class Thetask extends AsyncTask<String, Void, Void>
  605. {
  606.      ProgressDialog pg = null;
  607.      String x,y;        
  608.      @Override
  609.      public void onPreExecute()
  610.      ...