简体   繁体   English

ProgressDialog未出现在Android中

[英]ProgressDialog doesn't appear in Android

I am trying to have a ProgressDialog appear when I am getting some data, in Activity. 我试图在Activity中获取一些数据时出现一个ProgressDialog。 The activity runs normally, and the data are obtained, but the ProgressDialog does not appear. 活动正常运行,并且已获取数据,但不会显示ProgressDialog。

 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. @Ted Hopp建议使用AsyncTask。 I created the class GetISEPData and placed inside the dialog, but now the app gives me an error and closes. 我创建了类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();
    }       
}

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

In between the call to show() and the call to dismiss() , you need to do the work in a separate thread. 在对show()的调用与对dismiss()的调用之间,您需要在单独的线程中进行工作。 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. 看一下AsyncTask ,这是一种简单的方法。

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! 编辑:在更新的代码版本中,您有颠倒onPreExecute和onPostExecute的逻辑-您正试图在显示对话框之前关闭该对话框!

我认为它已显示,但由于将show()和dismiss()方法放置在同一线程中,因此也会立即将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM