2013-01-11 27 views
0

我調整了here的教程,通過JSON從YouTube頻道檢索視頻。一切工作正常,但我想添加ProgressDialog,而視頻加載在後臺線程。如果後臺線程由AsyncTask處理,我知道如何添加ProgressDialog,但我不知道如何在這種情況下執行此操作。如何使用處理程序在後臺線程中包裝ProgressDialog?

繼承人的代碼的重要組成部分:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_video); 

    listView = (VideosListView) findViewById(R.id.videosListView); 
    listView.setOnVideoClickListener(this); 

    getUserYouTubeFeed((VideosListView) findViewById(R.id.videosListView)); 
} 

// This is the XML onClick listener to retreive a users video feed 
public void getUserYouTubeFeed(View v){ 
    // We start a new task that does its work on its own thread 
    // We pass in a handler that will be called when the task has finished 
    // We also pass in the name of the user we are searching YouTube for 
    new GetYouTubeUserVideosTask(responseHandler, "videoslusofona").run(); 
} 

// This is the handler that receives the response when the YouTube task has finished 
Handler responseHandler = new Handler() { 
    public void handleMessage(Message msg) { 
     populateListWithVideos(msg); 
    }; 
}; 

所以我要列出上打開的視頻活動,以及最重要的是我在XML佈局,使用戶一鍵有android:onClick=getUserYouTubeFeed如果需要可以刷新。這個想法是將ProgressDialog放在某個地方,當活動開始時以及用戶按下刷新按鈕時,它將被激活。

如果你對我的啓發我應該把ProgressDialog放在哪裏,我將不勝感激。

在此先感謝。

編輯:

代碼與修改整個活動由PRATIK夏爾馬建議:

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 

import com.example.igestao.youtube.GetYouTubeUserVideosTask; 
import com.example.igestao.R; 
import com.example.igestao.youtube.Library; 
import com.example.igestao.youtube.Video; 
import com.example.igestao.youtube.VideoClickListener; 
import com.example.igestao.youtube.VideosListView; 

/** 
* The Activity can retrieve Videos for a specific username from YouTube</br> 
* It then displays them into a list including the Thumbnail preview and the title</br> 
* There is a reference to each video on YouTube as well but this isn't used in this  tutorial</br> 
* </br> 
* <b>Note<b/> orientation change isn't covered in this tutorial, you will want to  override 
* onSaveInstanceState() and onRestoreInstanceState() when you come to this 
* </br> 
* @author paul.blundell 
*/ 
public class MainVideoActivity extends Activity implements VideoClickListener { 
// A reference to our list that will hold the video details 
private VideosListView listView; 
ProgressDialog dialog = new ProgressDialog(MainVideoActivity.this); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_video); 

    listView = (VideosListView) findViewById(R.id.videosListView); 
    listView.setOnVideoClickListener(this); 

    getUserYouTubeFeed((VideosListView) findViewById(R.id.videosListView)); 
} 



// This is the XML onClick listener to retreive a users video feed 
public void getUserYouTubeFeed(View v){ 
    // We start a new task that does its work on its own thread 
    // We pass in a handler that will be called when the task has finished 
    // We also pass in the name of the user we are searching YouTube for 
    new VideosLoad().execute(); 
} 

// This is the handler that receives the response when the YouTube task has finished 
Handler responseHandler = new Handler() { 
    public void handleMessage(Message msg) { 
     if (dialog.isShowing()){ 
      dialog.dismiss(); 
     } 
     populateListWithVideos(msg); 
    }; 
}; 

/** 
* This method retrieves the Library of videos from the task and passes them to our ListView 
* @param msg 
*/ 
private void populateListWithVideos(Message msg) { 
    // Retreive the videos are task found from the data bundle sent back 
    Library lib = (Library) msg.getData().get(GetYouTubeUserVideosTask.LIBRARY); 
    // Because we have created a custom ListView we don't have to worry about setting the adapter in the activity 
    // we can just call our custom method with the list of items we want to display 
    listView.setVideos(lib.getVideos()); 
} 

@Override 
protected void onStop() { 
    // Make sure we null our handler when the activity has stopped 
    // because who cares if we get a callback once the activity has stopped? not me! 
    responseHandler = null; 
    super.onStop(); 
} 

@Override 
public void onVideoClicked(Video video) { 
    Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setData(Uri.parse(video.getUrl())); 
    startActivity(intent); 

} 

private class VideosLoad extends AsyncTask<Void, Void, String>{ 

    @Override 
    protected String doInBackground(Void... arg0){ 
     new GetYouTubeUserVideosTask(responseHandler, "videoslusofona").run(); 
     return "Executed"; 

    } 

    @Override 
    protected void onPreExecute() { 
     dialog.setMessage("Carregando Videos"); 
     dialog.show(); 
    } 

} 
} 
+0

嘗試使用我發佈的解決方案。 –

回答

0

所以,我終於得到它,它看起來像這樣:

的的AsyncTask:在處理程序

private class VideosLoad extends AsyncTask<Void, Void, String>{ 

private Context context; 

public VideosLoad(Context context) { 
    this.context = context; 
} 

@Override 
protected void onPreExecute() { 
    dialog = ProgressDialog.show(context, "", "Loading", true); 
} 


@Override 
protected String doInBackground(Void... arg0){ 
    new GetYouTubeUserVideosTask(responseHandler, "videoslusofona").run(); 
    return "Executed"; 

} 

駁回對話框:

Handler responseHandler = new Handler() { 
public void handleMessage(Message msg) { 
    if (dialog.isShowing()){ 
     dialog.dismiss(); 
    } 
    populateListWithVideos(msg); 
}; 
}; 

而且的onCreate

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_video); 

listView = (VideosListView) findViewById(R.id.videosListView); 
listView.setOnVideoClickListener(this); 

getUserYouTubeFeed((VideosListView) findViewById(R.id.videosListView)); 

} 



public void getUserYouTubeFeed(View v){ 

new VideosLoad(this).execute(); 

} 

感謝幫助Pratik Sharma

+0

不客氣。很高興聽到您解決問題。 –

0

嘗試是這樣的:

 Dialog = new ProgressDialog(yourActivity.this); 

     public void getUserYouTubeFeed(View v){ 
      new LongOperation().execute(); 
     } 

     // This is the handler that receives the response when the YouTube task has finished 
     Handler responseHandler = new Handler() { 
      public void handleMessage(Message msg) { 
       if (Dialog.isShowing()) { 
        Dialog.dismiss(); 
       } 
       populateListWithVideos(msg); 
      }; 
     }; 

聲明這個ASYC任務是:

private class LongOperation extends AsyncTask<Void, Void, String> { 
     @Override 
     protected String doInBackground(Void... arg0) { 
      new GetYouTubeUserVideosTask(responseHandler, "videoslusofona").run(); 
      return "Executed"; 
     }  
     @Override 
     protected void onPreExecute() { 
      Dialog.setMessage("Loading"); 
      Dialog.show(); 
     } 
} 
+0

我試過了,我得到一個錯誤:'01-11 12:03:18.072:E/AndroidRuntime(1444):java.lang.RuntimeException:無法實例化活動ComponentInfo {com.example.igestao/com.example。 igestao.MainVideoActivity}:java.lang.IllegalStateException:系統服務不可用於onCreate()之前的活動''我將編輯我的原始帖子,修改完整個活動。 – jsfrocha

+0

@jsfrocha在onCreate()方法下使用這個聲明'dialog = new ProgressDialog(LayoutTest.this);'。 –

+0

@jsfrocha只在onCreate()'ProgressDialog對話框;'之前的頂部聲明它。 –

相關問題