2012-06-13 32 views
0

嗨,我有一個AysncTask,我想要在完成加載視圖時展開加載視圖,並將其隱藏起來。有誰知道如何做到這一點?目前它沒有顯示加載視圖。Android在AsyncTask中顯示和隱藏視圖

這是我曾嘗試

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    loadingView = LayoutInflater.from(getBaseContext()).inflate(R.layout.loadingcell, 
      null); 

    //Check Preferences which sets UI 

    checkPreferences(); 
    PostTask posttask; 
    posttask = new PostTask(); 
    posttask.execute(); 

} 




    @Override 
    protected Boolean doInBackground(Void... params) { 
     boolean result = false; 


     loadFixtures(); 
     publishProgress("progress"); 
     loadResultsFeed(); 
     publishProgress("progress"); 
     loadNewsFeed(); 
     publishProgress("progress"); 
     return result; 
    } 


    protected void onProgressUpdate(String... progress) { 
     StringBuilder str = new StringBuilder(); 
      for (int i = 1; i < progress.length; i++) { 
       str.append(progress[i] + " "); 



      } 
    } 

     @Override 
    protected void onPostExecute(Boolean result) { 
     super.onPostExecute(result); 
     Log.v("BGThread", "begin fillin data"); 
     FillData(); 
     loadingView.setVisibility(View.GONE); 
     } 
} 

loadingcell.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/white" 
    android:id="@+id/loadingcell"> 


    <include 
     layout="@layout/header" /> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     style="?android:attr/progressBarStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="87dp" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="115dp" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@+id/progressBar1" 
     android:gravity="center" 
     android:text="Loading..." 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="@color/blue" /> 

</RelativeLayout> 
+0

[參見本(http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html) –

+0

不重複烏爾Que..duplicate。 http://stackoverflow.com/q/11013505/1289716 – MAC

+0

@SamirMangroliya嗨Samir有沒有一種方法,我可以做到這一點,而不使用對話框我只是想要一個頁面,有一個文本視圖,說裝載和一個微調,然後postexecute讓它隱藏起來 –

回答

1

你是不是在你的代碼中使用AsyncTask。使用這樣的事情:

private class doSomething extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     //Inflate you view or do anything here before loading stuff 
    } 

    protected Void doInBackground(Void... urls) { 
     //loading stuff 
    } 

    protected void onProgressUpdate(String... progress) { 
     //change your UI - like a progressbar 
    } 

    @Override 
    protected void onPostExecute(String unused) { 
     //Hide the progressbar 
    }