0

我从这里改编了一个教程,通过 JSON 从 YouTube 频道检索视频。一切正常,但我想在后台线程中加载视频时添加一个 ProgressDialog。如果后台线程由 AsyncTask 处理,我知道如何添加 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);
    };
};

所以我有一个活动列出打开的视频,最重要的是,我android:onClick=getUserYouTubeFeed在 XML 布局中有一个按钮,以便用户可以在需要时刷新。想法是将 ProgressDialog 放在某个位置,以便在活动启动以及用户按下刷新按钮时将其激活。

如果您能告诉我应该将 ProgressDialog 放在哪里,我将不胜感激。

提前致谢。

编辑:

整个活动的代码以及 Pratik Sharma 建议的修改:

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();
    }

}
}
4

2 回答 2

0

所以我终于明白了,它看起来像这样:

异步任务:

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

于 2013-01-11T18:03:47.970 回答
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();
        }
}  
于 2013-01-11T10:48:03.483 回答