自定义对话框片段中的进度条 - 如何从AsyncTask传递进度?

时间:2015-04-19 12:12:10

标签: android android-fragments android-asynctask callback progress-bar

我创建了一个自定义对话框片段,在其中放置了一个进度条。主机活动有一个AsyncTask,它将一些图像上传到服务器。我需要的是一种将进度传递给进度对话框片段的方法,我已尝试通过变量,但随着进度的变化,它会一次又一次地启动fragmet。

如果值在变化时如何在不重新启动对话框片段的情况下传递进度值?

我也试过了一个接口,但它不会初始化我的监听器(监听器总是为null)。

更新 我通过在asyncTask的onStart()方法中启动对话框片段来解决我的问题。我在onProgeress()方法中创建了另一个方法,它将进度值传递给已经打开的警告对话框:

            @Override
            public void onProgress(int position, int length) {
                ((PreloaderDialog) newFragment).passValues(position,
                        length);
            }

            @Override
            public void onStart() {
                // Show preloader dialog
                newFragment.show(getSupportFragmentManager(),
                        "preloader");
            }

在我的AlertDialog片段中:

public void passValues(int position, int length){
        uploadProgress.setMax(length);
        uploadProgress.setProgress(position);
    }

旧代码:

我的asyncTask(loopj):

private UploadProgressListener progressListener;

    public interface UploadProgressListener { 
        public void onProgress(int position, int length);       
    }

    //Interface to send selected image's position for deletion
    public void setUploadProgressListener(UploadProgressListener listener) {  
        this.progressListener = listener;  
    } 

...

    client.post("http://www.edmondvarga.com/laborator/upload.php", params,
                    new AsyncHttpResponseHandler() {

                        @Override
                        public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                                Throwable arg3) {
                            System.out.println("Upload failed!" + arg3
                                    + " statys code: " + arg0);
                            if (arg0 == 0) {
                                System.out.println("Retrying Upload!");
                                sendZip();
                            }
                        }

                        @Override
                        public void onProgress(int position, int length) {

                            if(progressListener != null){
                            progressListener.onProgress(position, length);
                            }

                        }

                        @Override
                        public void onStart() {

                            // Show preloader dialog
                            DialogFragment newFragment = new PreloaderDialog();
                            newFragment.show(getSupportFragmentManager(),
                                    "preloader");
                        }

                        @Override
                        public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                            System.out.println("Success!");
                            Toast.makeText(context, "Multumim pentru Comanda!",
                                    Toast.LENGTH_LONG).show();
                            finish();
                            datasource.deleteAllRows();
                        }
                    });

和进度对话框片段:

public class PreloaderDialog extends DialogFragment implements UploadProgressListener {

    private Builder v;
    private ProgressBar uploadProgress;
    int position;
    int length;

    /*
    public PreloaderDialog() {
        this.position = position;
        this.length = length;
    }
    */

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.preloader_dialog, null);

        builder.setView(view);

        OrderActivity progressChanged = new OrderActivity();
        progressChanged.setUploadProgressListener(new UploadProgressListener() {

            @Override
            public void onProgress(int position, int length) {
                System.out.println("changed");

            }
        });

        uploadProgress = (ProgressBar) view.findViewById(R.id.uploadProressbar);

        // Create the AlertDialog object and return it
        return builder.create();
    }


    @Override
    public void onProgress(int position, int length) {
        //uploadProgress.setMax(length);
       // uploadProgress.setProgress(position);
        //System.out.println("progresspos: " + position);

    }

1 个答案:

答案 0 :(得分:0)

手动创建活动是个坏主意,这可能是您问题的根源。

OrderActivity progressChanged = new OrderActivity();

您可能需要将活动作为参数传递或投射getContext();我也不知道为什么您需要在那里进行活动。目标侦听器应该是对话框,您应该在活动中设置侦听器,而不是arround。