如何更新ASYNCTASK中的对话框元素的进度栏?

发布于 2025-01-31 15:08:28 字数 2069 浏览 4 评论 0原文

我目前正在搜索如何更新位于对话框窗口中的进度栏​​。 我已经对此有多个帖子,但是我不明白为什么我的进度栏不会更新。

这是我的代码:

public class Dataset1Fragment extends Fragment {
private static final int APP_SMS_DEFAULT = 5;
DatabaseHelper myDb;
ProgressBar progressBar;
Dialog dialog;

private Button btnInsert;
private Button btnDelete;
String[] pictures = null;
private RelativeLayout lay_dataset1;

@SuppressLint("InflateParams")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.progress, null);
    progressBar = view.findViewById(R.id.loader);
    return inflater.inflate(R.layout.fragment_dataset1, null);
  }
}

这是片段的一部分,我刚刚称为progressbar

,这部分是我的异步键:

 // ASYNC TASK POUR DELETE
private class ExampleAsyncTaskDelete extends AsyncTask<Integer, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        setDialog(true);
        progressBar.setMax(100);
        Log.d("DELETE THREAD", "PreExecute");
    }

    @Override
    protected String doInBackground(Integer... integers) {

        for (int i=0; i<100;i++){
            publishProgress(i);
            try {
                Thread.sleep(100);
            }catch (InterruptedException ie){
                ie.printStackTrace();
            }
        }
        return "Finished!";
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressBar.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Toast.makeText(requireContext(), "Delete ok", Toast.LENGTH_SHORT).show();
        setDialog(false);
        requireActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    }
}

public void setDialog(boolean show) {
    if (show) dialog.show();
    else dialog.dismiss();
}

这些部分在同一文件中。 在“对话框”中拥有进度栏并非不可能吗? 我肯定想念STH,但是什么?

I'm currently searching how to update my progress bar which is located in my dialog window.
I have already multiple post on this but I can't understand why my progress bar won't update.

Here is my code :

public class Dataset1Fragment extends Fragment {
private static final int APP_SMS_DEFAULT = 5;
DatabaseHelper myDb;
ProgressBar progressBar;
Dialog dialog;

private Button btnInsert;
private Button btnDelete;
String[] pictures = null;
private RelativeLayout lay_dataset1;

@SuppressLint("InflateParams")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.progress, null);
    progressBar = view.findViewById(R.id.loader);
    return inflater.inflate(R.layout.fragment_dataset1, null);
  }
}

This is the part of fragment where I just called the progressBar

And this part is my AsyncTask :

 // ASYNC TASK POUR DELETE
private class ExampleAsyncTaskDelete extends AsyncTask<Integer, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        setDialog(true);
        progressBar.setMax(100);
        Log.d("DELETE THREAD", "PreExecute");
    }

    @Override
    protected String doInBackground(Integer... integers) {

        for (int i=0; i<100;i++){
            publishProgress(i);
            try {
                Thread.sleep(100);
            }catch (InterruptedException ie){
                ie.printStackTrace();
            }
        }
        return "Finished!";
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressBar.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Toast.makeText(requireContext(), "Delete ok", Toast.LENGTH_SHORT).show();
        setDialog(false);
        requireActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    }
}

public void setDialog(boolean show) {
    if (show) dialog.show();
    else dialog.dismiss();
}

The those part are in the same file.
It is not impossible to have a progress bar in a "dialog" right ?
I missed sth for sure but what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文