Android 中不出现 ProgressDialog

发布于 2024-12-26 16:20:01 字数 5097 浏览 2 评论 0原文

当我在 Activity 中获取一些数据时,我试图显示 ProgressDialog。 Activity正常运行,并且获取到数据,但是没有出现ProgressDialog。

 private void updateData() {
        ProgressDialog dialog = ProgressDialog.show(ParkActivity.this, "fvhnn", "A actualizar. Aguarde por favor...", true);
        ...
        ...
        dialog.dismiss();
        ...
 }

按照我的活动代码。

public class ParkActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.park);

        SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
        int ocupados = settings.getInt("ocupados", -1);

        if(ocupados == -1) {
            ((TextView) findViewById(R.id.txtPercentageOcupation)).setVisibility(View.INVISIBLE);
            ((TextView) findViewById(R.id.txtLastUpdate)).setVisibility(View.INVISIBLE);
            ((TextView) findViewById(R.id.txtPlacesAvailable)).setVisibility(View.INVISIBLE);
            ((Button) findViewById(R.id.btnParkUpdate)).setVisibility(View.INVISIBLE);

            updateData();
        } else {
            int total = settings.getInt("total", 440);
            long data = settings.getLong("data", 0);

            putData(ocupados, total, data);
        }


        Button btnUpdate = (Button) findViewById(R.id.btnParkUpdate);
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                updateData();
            }
        });
    }

    private void updateData() {
        ProgressDialog dialog = ProgressDialog.show(ParkActivity.this, "fvhnn", "A actualizar. Aguarde por favor...", true);
        SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
        String session = settings.getString("sessionID", "null");
        int[] vagas = ISEP_Proxy.vagas(session);
        Date dt = new Date();
        putData(vagas[0], vagas[1], dt.getTime());
        dialog.dismiss();

        // save data
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("ocupados", vagas[0]);
        editor.putInt("total", vagas[1]);
        editor.putLong("data", dt.getTime());

        editor.commit();
    }

    private void putData(int ocupados, int total, long date) {
        String placesFormat = getResources().getString(R.string.PLACES_AVAILABLE);
        String percentageFormat = getResources().getString(R.string.PERCENTAGE_OCUPATION);
        String lastUpdate = getResources().getString(R.string.LAST_UPDATE);
        double percentage =  100.0 * ocupados / total;

        SimpleDateFormat df = new SimpleDateFormat("HH'h'mm 'de' dd-MM-yyyy");

        ((TextView) findViewById(R.id.txtPercentageOcupation)).setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.txtLastUpdate)).setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.txtPlacesAvailable)).setVisibility(View.VISIBLE);
        ((Button) findViewById(R.id.btnParkUpdate)).setVisibility(View.VISIBLE);

        ((TextView) findViewById(R.id.txtPercentageOcupation)).setText(String.format(percentageFormat, ((int) percentage) + "%"));
        ((TextView) findViewById(R.id.txtPlacesAvailable)).setText(String.format(placesFormat, total - ocupados));
        ((TextView) findViewById(R.id.txtLastUpdate)).setText(String.format(lastUpdate, df.format(date)));
    }
}

有人知道问题是什么吗?


编辑

@Ted Hopp 建议使用 AsyncTask。 我创建了 GetISEPData 类并将其放置在对话框中,但现在应用程序给出了错误并关闭。

private class GetISEPData extends AsyncTask<Void, Void, Void> {
    Context cx;
    ProgressDialog dialog;

    public GetISEPData (Context context) {
        cx = context;
    }

    @Override
    protected Void doInBackground(Void... params) {
        SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
        String session = settings.getString("sessionID", "null");
        int[] vagas = ISEP_Proxy.vagas(session);
        Date dt = new Date();
        putData(vagas[0], vagas[1], dt.getTime());

        // save data
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("ocupados", vagas[0]);
        editor.putInt("total", vagas[1]);
        editor.putLong("data", dt.getTime());

        editor.commit();            
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dialog = ProgressDialog.show(ParkActivity.this, "", "A actualizar. Aguarde por favor...", true);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog.dismiss();
    }       
}

对于调用,请输入:

    Button btnUpdate = (Button) findViewById(R.id.btnParkUpdate);
    btnUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            new GetISEPData(ParkActivity.this).execute();
        }
    });

I am trying to have a ProgressDialog appear when I am getting some data, in Activity.
The activity runs normally, and the data are obtained, but the ProgressDialog does not appear.

 private void updateData() {
        ProgressDialog dialog = ProgressDialog.show(ParkActivity.this, "fvhnn", "A actualizar. Aguarde por favor...", true);
        ...
        ...
        dialog.dismiss();
        ...
 }

follow the code of my Activity.

public class ParkActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.park);

        SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
        int ocupados = settings.getInt("ocupados", -1);

        if(ocupados == -1) {
            ((TextView) findViewById(R.id.txtPercentageOcupation)).setVisibility(View.INVISIBLE);
            ((TextView) findViewById(R.id.txtLastUpdate)).setVisibility(View.INVISIBLE);
            ((TextView) findViewById(R.id.txtPlacesAvailable)).setVisibility(View.INVISIBLE);
            ((Button) findViewById(R.id.btnParkUpdate)).setVisibility(View.INVISIBLE);

            updateData();
        } else {
            int total = settings.getInt("total", 440);
            long data = settings.getLong("data", 0);

            putData(ocupados, total, data);
        }


        Button btnUpdate = (Button) findViewById(R.id.btnParkUpdate);
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                updateData();
            }
        });
    }

    private void updateData() {
        ProgressDialog dialog = ProgressDialog.show(ParkActivity.this, "fvhnn", "A actualizar. Aguarde por favor...", true);
        SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
        String session = settings.getString("sessionID", "null");
        int[] vagas = ISEP_Proxy.vagas(session);
        Date dt = new Date();
        putData(vagas[0], vagas[1], dt.getTime());
        dialog.dismiss();

        // save data
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("ocupados", vagas[0]);
        editor.putInt("total", vagas[1]);
        editor.putLong("data", dt.getTime());

        editor.commit();
    }

    private void putData(int ocupados, int total, long date) {
        String placesFormat = getResources().getString(R.string.PLACES_AVAILABLE);
        String percentageFormat = getResources().getString(R.string.PERCENTAGE_OCUPATION);
        String lastUpdate = getResources().getString(R.string.LAST_UPDATE);
        double percentage =  100.0 * ocupados / total;

        SimpleDateFormat df = new SimpleDateFormat("HH'h'mm 'de' dd-MM-yyyy");

        ((TextView) findViewById(R.id.txtPercentageOcupation)).setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.txtLastUpdate)).setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.txtPlacesAvailable)).setVisibility(View.VISIBLE);
        ((Button) findViewById(R.id.btnParkUpdate)).setVisibility(View.VISIBLE);

        ((TextView) findViewById(R.id.txtPercentageOcupation)).setText(String.format(percentageFormat, ((int) percentage) + "%"));
        ((TextView) findViewById(R.id.txtPlacesAvailable)).setText(String.format(placesFormat, total - ocupados));
        ((TextView) findViewById(R.id.txtLastUpdate)).setText(String.format(lastUpdate, df.format(date)));
    }
}

Anyone know what the problem is?


EDIT

@Ted Hopp Suggest to use a AsyncTask.
I created the class GetISEPData and placed inside the dialog, but now the app gives me an error and closes.

private class GetISEPData extends AsyncTask<Void, Void, Void> {
    Context cx;
    ProgressDialog dialog;

    public GetISEPData (Context context) {
        cx = context;
    }

    @Override
    protected Void doInBackground(Void... params) {
        SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
        String session = settings.getString("sessionID", "null");
        int[] vagas = ISEP_Proxy.vagas(session);
        Date dt = new Date();
        putData(vagas[0], vagas[1], dt.getTime());

        // save data
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("ocupados", vagas[0]);
        editor.putInt("total", vagas[1]);
        editor.putLong("data", dt.getTime());

        editor.commit();            
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dialog = ProgressDialog.show(ParkActivity.this, "", "A actualizar. Aguarde por favor...", true);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog.dismiss();
    }       
}

for invoke put this:

    Button btnUpdate = (Button) findViewById(R.id.btnParkUpdate);
    btnUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            new GetISEPData(ParkActivity.this).execute();
        }
    });

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深爱不及久伴 2025-01-02 16:20:01

在调用 show() 和调用 dismiss() 之间,您需要在单独的线程中完成工作。在您将控制权返回给框架之前,对话框没有机会被绘制。查看 AsyncTask 了解执行此操作的简单方法。

编辑:在代码的更新版本中,onPreExecute 和 onPostExecute 的逻辑颠倒了——您试图在显示之前关闭对话框!

In between the call to show() and the call to dismiss(), you need to do the work in a separate thread. Until you return control to the framework, the dialog doesn't have a chance to get drawn. Take a look at AsyncTask for an easy way to do this.

EDIT: In your updated version of your code, you have the logic for onPreExecute and onPostExecute reversed -- you are trying to dismiss the dialog before it is shown!

半葬歌 2025-01-02 16:20:01

我认为它显示了,但它也立即被解雇,因为 show() 和解雇() 方法被放置在同一个线程中。

I think it shown, but it also dismissed immediately cause the show() and dismiss() methods are placed in same thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文