2012-02-23 47 views
0

我從Web服務中獲取GridView的圖像和文本,因此需要一些時間才能顯示GridView。我想顯示ProgressDialog,直到Grid完全加載。我做了什麼至今如下是:使用ProgressDialog直到從webservice加載GridView

public class PCGridMain extends Activity 
{ 
    WebService web = new WebService(); 
    private GridView gridView; 
    ProgressDialog dialog; 
    Bitmap icon; 


    int i, total; 
    URL url= null; 
    List<GridItem> list; 

    @Override 
    public void onCreate(Bundle grid) 
    { 
     super.onCreate(grid); 
     dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", true); 
     DialogWork dWork = new DialogWork(); 
     dWork.execute(); 

     setContentView(R.layout.main); 
     gridView = (GridView)findViewById(R.id.gridView1); 

     web.WebService1();  
     total = web.totalService;   



     list = new ArrayList<GridItem>(); 

     for(i=0; i<total; i++) 
      { 
        Log.v("Try Block", "See what we get:-"); 
        try 
        { 
         Log.v("GridMain", "try url" + Integer.toString(i)); 
         url = new URL(web.arr[i][2]); 
        } 
        catch (MalformedURLException e) 
        { 
         Log.v("GridMain", "catch MalformedURLException" + Integer.toString(i)); 
         e.printStackTrace(); 
        } 


        try 
        { 
         Log.v("GridMain", "try BitmapFactory" + Integer.toString(i)); 
         icon = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
        } 
        catch (IOException e) 
        { 
         Log.v("GridMain", "catch IOException" + Integer.toString(i)); 
         e.printStackTrace(); 
        } 

        list.add(new GridItem(icon, web.arr[i][1]));     // Adding Icon & LAbel    

      } 

     gridView.setAdapter(new GridAdapter(this, list)); 
     gridView.setOnItemClickListener(Itemlistener); 
    } 



    private OnItemClickListener Itemlistener = new OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, int position, long id) 
     { 
      ViewHolder holder = (ViewHolder)view.getTag(); 

      if(holder == null) 
      { 
       return; 
      } 
      Toast.makeText(PCGridMain.this, holder.label.getText(), Toast.LENGTH_SHORT).show(); 
                       Log.v("GridMain", "Intent Creation"); 
      Intent intent = new Intent(view.getContext(), ShowService.class); Log.v("GridMain", "Intent Created"); 
      intent.putExtra("ServiceId", web.arr[position][0]);     Log.v("GridMain", "ValueAdded Sid"); 
      intent.putExtra("SName", holder.label.getText());     Log.v("GridMain", "ValueAdded SName"); 

      startActivity(intent);   

     } 
    }; 

    class DialogWork extends AsyncTask<URL, Integer, Long> 
    {  
     protected Long doInBackground(URL... params) 
     { 
      try 
      { 

      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     protected void onProgressUpdate(Integer... progress) 
     { 

     } 

     protected void onPostExecute(Long result) 
     { 
      try 
      { 
       //setContentView(R.layout.main); 
       //gridView.setAdapter(new GridAdapter(PCGridMain.this, list));    
       dialog.dismiss();     
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       dialog.dismiss(); 
      } 
     } 

    } 

請告訴我什麼代碼已經被放置在什麼確切的位置,每當我做了一些變化,它要麼顯示沒有影響或應用程序關閉,由於一些問題。

謝謝,

Haps。

+0

你應該在AsyncTask中顯示'ProgressDialog'。在任務啓動之前實現'onPreExecute'方法並初始化對話框。 – 2012-02-23 13:50:43

+0

從應用程序用戶的角度來看,通過ProgressDialog阻止整個GridView是一個非常糟糕的設計。用戶應該有能力查看/點擊部分GridView,而其他部分仍在加載。考慮使用AsyncTask.onProgressUpdate()按需顯示加載的圖像,而不是阻止整個GridView,並最終在所有圖像加載後顯示它。 – yorkw 2012-02-23 21:57:46

回答

5

嘗試將doInBackground()中的服務器的所有渲染部分放在onPostExecute()中。甚至在onPreExecute()中啓動progressdialog,並在onPostExecute()上關閉它,但不在onCreate()中關閉。我認爲這將解決烏爾問題....

+0

是的,我真的做到了,感謝您的幫助。 – Harpreet 2012-02-28 06:32:44

2

與此代碼別的代碼嘗試對話似乎工作

dialog= new ProgressDialog(this); 
       dialog.setMessage("Loading"); 
       dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
       dialog.setCancelable(false);   
       dialog.show(); 
+0

這個概念也很好,我們也可以使用它。 – Harpreet 2012-04-20 09:50:23

4

這應該是你的內心AsyncTask類,更改參數,因爲你需要。

private class yourTask extends AsyncTask<Void, Void, ArrayList> { 

    String message; 
    ProgressDialog dialog; 

    public refreshTask(String message) { 
     this.message = message; 
     this.dialog = new ProgressDialog(PCGridMain.this); 
    } 

    @Override 
    protected void onPreExecute() { 
     dialog.setMessage(message); 
     dialog.setIndeterminate(true); 
     dialog.setCancelable(true); 
     dialog.show(); 
    } 

    @Override 
    protected ArrayList doInBackground(String... params) { 
     // Some work 
    } 

    @Override 
    protected void onPostExecute(ArrayList result) { 
     if(dialog.isShowing()) 
        dialog.dismiss(); 
    } 
} 

所以,你可以調用這個類,如:

new yourTask('Dialog message').execute(); 

我希望它幫您解決問題。

+0

對不起兄弟,得到了致命的例外:主。 – Harpreet 2012-02-28 06:16:46

+0

感謝您的幫助。 – Harpreet 2012-02-28 06:33:19

+0

@發生什麼問題? – 2012-02-28 09:53:20

3

這裏我給完整的回答我的問題,因此,它可以幫助別人,使之輕鬆完成......

public class PCGridMain extends Activity 
{ 
    WebService web = new WebService(); 
    private GridView gridView; 
    ProgressDialog dialog; 
    Bitmap icon; 


int i, total; 
URL url= null; 
List<GridItem> list; 

@Override 
public void onCreate(Bundle grid) 
{ 
    super.onCreate(grid); 

    Log.v("GridMain", "setContent");  
    setContentView(R.layout.main); 
    gridView = (GridView)findViewById(R.id.gridView1); 

    DialogWork dWork = new DialogWork(); 
    dWork.execute(); 

} 



private void ForLoop() 
{ 

    for(i=0; i<total; i++) 
     { 
       Log.v("Try Block", "See what we get:-"); 
       try 
       { 
        Log.v("GridMain", "try url" + Integer.toString(i)); 
        url = new URL(web.arr[i][2]); 
       } 
       catch (MalformedURLException e) 
       { 
        Log.v("GridMain", "catch MalformedURLException" + Integer.toString(i)); 
        e.printStackTrace(); 
       } 


       try 
       { 
        Log.v("GridMain", "try BitmapFactory" + Integer.toString(i)); 
        icon = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
       } 
       catch (IOException e) 
       { 
        Log.v("GridMain", "catch IOException" + Integer.toString(i)); 
        e.printStackTrace(); 
       } 

       list.add(new GridItem(icon, web.arr[i][1]));     // Adding Icon & LAbel    

     }  

} 



private OnItemClickListener Itemlistener = new OnItemClickListener() 
{ 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View view, int position, long id) 
    { 
     ViewHolder holder = (ViewHolder)view.getTag(); 

     if(holder == null) 
     { 
      return; 
     } 
     Toast.makeText(PCGridMain.this, holder.label.getText(), Toast.LENGTH_SHORT).show(); 
                      Log.v("GridMain", "Intent Creation"); 
     Intent intent = new Intent(view.getContext(), ShowService.class); Log.v("GridMain", "Intent Created"); 
     intent.putExtra("ServiceId", web.arr[position][0]);     Log.v("GridMain", "ValueAdded Sid"); 
     intent.putExtra("SName", holder.label.getText());     Log.v("GridMain", "ValueAdded SName"); 

     startActivity(intent);   

    } 
}; 


class DialogWork extends AsyncTask<URL, Integer, String> 
{ 
    protected void onPreExecute() 
    { 
     Log.v("GridMain", "PreExecute()"); 
     dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", false, true); 

    } 
    protected Long doInBackground(URL... params) 
    { 
        String response = ""; 
     try 
     { 
      Log.v("GridMain", "doInBackground"); 
      response = web.WebService1(); 
      total = web.totalService; 

     } 
     catch (InterruptedException e) 
     { 
      Log.v("GridMain", "InterruptedException"); 
      e.printStackTrace(); 
     }   
     return response; 
    }  

    protected void onPostExecute(String result) 
    { 
     try 
     { 
          // Response is in RESULT_VAR 
      Log.v("GridMain", "onPostExecute"); 
      list = new ArrayList<GridItem>(); 
      ForLoop(); 
      gridView.setAdapter(new GridAdapter(PCGridMain.this, list)); 
      gridView.setOnItemClickListener(Itemlistener); 
      dialog.dismiss(); 
     } 
     catch (Exception e) 
     { 
      Log.v("GridMain", "Exception e"); 
      e.printStackTrace(); 
      dialog.dismiss(); 
     } 
    } 

} 
} 

我已經用它作爲根據我的需要,它只是爲幫助,完整的代碼可能會給你帶來問題。所以請把它當作參考。

感謝&問候,

HAPS。