2013-05-21 50 views
0

我正在一個android應用程序中,我想顯示進度對話框,直到加載gridview完成。但我的問題是進展對話在一些初始時間旋轉。然後停止旋轉。進度對話框應旋轉,直到gridview加載完成

這是我的代碼。

public class allsites extends Activity { 

    private final String url_select = "http://api.stackexchange.com/2.1/sites?filter=!RGB_Y51.*-(YX"; 
    private GridView gview; 
    private ListViewCustomAdapter adapter; 
    private ArrayList<Object> itemList = new ArrayList<Object>(); 
    private ItemBean bean; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.allsites); 
     //GridView gridview = (GridView) findViewById(R.id.gvAllSites); 
     gview = (GridView) findViewById(R.id.gvallsites); 
     new task().execute(); 


    } 

    private class task extends AsyncTask<Void, Void, GZIPInputStream> { 

     private ProgressDialog progress; 


     @Override 
     protected void onPreExecute() { 

      progress = ProgressDialog.show(allsites.this, "Loading", "Please Wait..."); 
     } 

     @Override 
     protected GZIPInputStream doInBackground(Void... params) { 

      ServerData httpclient = new ServerData(); 
      GZIPInputStream zis = httpclient.GetServerData(url_select); 
      return zis; 
     } 

     @Override 
     protected void onPostExecute(GZIPInputStream zis) { 


      ParseJSON(zis); 

      if(progress!=null && progress.isShowing()==true) 
       progress.dismiss(); 



     } 
    } 

    private void ParseJSON(GZIPInputStream zis) 
    { 
     Gson gson = new Gson(); 

     Reader reader = new InputStreamReader(zis); 

     Sites response = gson.fromJson(reader, Sites.class); 

     List<Items> items = response.getItems(); 

     for (Items site : items) { 
      //Toast.makeText(allsites.this, site.getApi_site_parameter().toString(), Toast.LENGTH_SHORT).show(); 
      AddObjectToList(site.getIcon_url(),site.getName()); 
     } 

     adapter = new ListViewCustomAdapter(this, itemList); 
     gview.setAdapter(adapter); 
    } 

    public void AddObjectToList(String imageURL, String title) 
    { 
     bean = new ItemBean(); 

     try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent()); 
      bean.setImage(bitmap); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     bean.setTitle(title); 
     itemList.add(bean); 
    } 

} 

請給我一個建議,我可以讓進度對話框旋轉,直到gridview得到加載。

+0

您可能需要移動'ParseJSON(ZIS);'部分'doInBackground()'爲好。此外,代碼似乎在'dismiss()'後面缺少一個右大括號。 –

回答

0

移動ParseJSON功能doInBackground事件

@Override 
    protected Boolean doInBackground(Void... params) { 

     ServerData httpclient = new ServerData(); 
     GZIPInputStream zis = httpclient.GetServerData(url_select); 

     ParseJSON(zis); 
     return true; 
    } 

    @Override 
    protected void onPostExecute(Boolean zis) { 

      progress.dismiss(); 
    } 
相關問題