2012-06-08 37 views
1

在我的申請,我有一個ProgressDialog顯示,而應用程序做一些事情:Android的Facebook的等待AsyncFacebookRunner完成?

mProgressDialog = ProgressDialog.show(
      ((FriendListActivity) ctx).getParent(), "Please wait...", 
      "Getting data...", true); 
    updateDisplay(true); 

在updateDisplay方法,它做些事情是這樣的:

items = new ArrayList<FriendInfo>(); 
    fa = new FriendListAdapter(ctx, R.layout.friendlist_item, items); 
    setListAdapter(fa); 

Thread t = new Thread() { 
      public void run() { 
       getFriendList(); //This is where the problem occured 

       initFriendList(); 
       handler.post(new Runnable() { 
        public void run() { 
         mProgressDialog.dismiss(); 
         fa.notifyDataSetChanged(); 
        }; 
       }); 
      } 
     }; 
     t.start(); 

getFriendList(),我撥打電話獲得Facebook用戶信息:

AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(
        facebook); 
      mAsyncRunner.request("me/friends&fields=name,picture", 
        new FriendsRequestListener((FriendListActivity) ctx, 
          currentuser)); 

之後,用戶的信息將被保存在我的數據庫中。並且initFriendList();方法(在getFriendList()下面)將使用該數據來初始化視圖。

問題是我想initFriendList()等到getFriendList()完成獲取數據。但在getFriendList()中,我使用AsyncFacebookRunner,因此initFriendList()將立即運行。我如何讓initFriendList()在運行之前等待AsyncFacebookRunner完成。

回答

0

我是新來的android世界,也許這些可以幫助嗎?

http://developer.android.com/guide/components/processes-and-threads.html#AsyncTask

具體的AsyncTask

  1. 您可以指定參數,進度值,任務的最終值,使用泛型類型。
  2. 方法doInBackground()在工作線程上自動執行。
  3. onPreExecute(),onPostExecute()和onProgressUpdate()都在UI線程上調用。
  4. doInBackground()返回的值被髮送到onPostExecute()。
  5. 您可以隨時在doInBackground()中調用publishProgress()以在UI線程上執行onProgressUpdate()。
  6. 您可以隨時從任何線程取消任務。

解釋真的很好這裏..

http://www.youtube.com/watch?v=uzJmi59b6oI&feature=bf_next&list=PL3B389D29207777C7

在正確的軌道?

-