在转到另一个Activity类之前,Progress Dialog没有出现在我的Activity中

时间:2017-11-20 14:58:12

标签: java android progressdialog

当我点击按钮并且进度对话框没有显示并直接显示给其他活动时,我遇到了问题。我想要做的是,在我去其他活动之前出现进度对话框。我正在尝试获得一个很好的弹出对话框,显示何时单击btn_create但它没有显示。有人有解决方案吗?

这是我目前的代码:

DateFormat formatDateTime = DateFormat.getDateTimeInstance();
Calendar mCurrentDate = Calendar.getInstance();

private EditText mEventTitle;
private EditText mEventLoc;
private EditText mEventDesc;
private TextView text_date;
private TextView text_time;
private ImageButton btn_date;
private ImageButton btn_time;
private Button btn_create;
private Button btn_discard;
private int day,month,year,hour,min;

private DatabaseReference mEventDatabase;
private DatabaseReference mUsersDatabase;
private FirebaseUser mCurrentUser;

private ProgressDialog mPDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_new_event);

    mEventTitle = (EditText) findViewById(R.id.eventTitle);
    mEventLoc=(EditText)findViewById(R.id.eventLocation);
    mEventDesc=(EditText)findViewById(R.id.eventDesc);
    text_date = (TextView) findViewById(R.id.eventDate);
    text_time = (TextView) findViewById(R.id.eventTime);
    btn_date = (ImageButton) findViewById(R.id.pickDateBtn);
    btn_time = (ImageButton) findViewById(R.id.pickTimeBtn);
    btn_create=(Button)findViewById(R.id.createEvent);
    btn_discard=(Button)findViewById(R.id.discardEvent);

    mPDialog = new ProgressDialog(AddNewEvent.this);

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
    mEventDatabase = FirebaseDatabase.getInstance().getReference().child("Event");
    mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(mCurrentUser.getUid());



    btn_create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            updateEvent();
        }
    });


}



private void updateEvent() {

    mPDialog.setTitle("Posting...");
    mPDialog.setMessage("Please wait...");
    mPDialog.setCanceledOnTouchOutside(false);

    String d = text_date.getText().toString();
    String t = text_time.getText().toString();
    final String dt = d+"  "+t;

    final String title_val = mEventTitle.getText().toString().trim();
    final String loc_val = mEventLoc.getText().toString().trim();
    final String desc_val = mEventDesc.getText().toString().trim();
    final String current_uid = mCurrentUser.getUid();

    if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(loc_val) && !TextUtils.isEmpty(desc_val) && !TextUtils.isEmpty(dt)){

        mPDialog.show();

        final DatabaseReference newEvent = mEventDatabase.push();

        SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy 'at' hh:mma");
        Date date = new Date();
        final String currentDate = df.format(date);

        mUsersDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                newEvent.child("title").setValue(title_val);
                newEvent.child("location").setValue(loc_val);
                newEvent.child("desc").setValue(desc_val);
                newEvent.child("date&time").setValue(dt);
                newEvent.child("uid").setValue(current_uid);
                newEvent.child("postDate").setValue(currentDate);
                newEvent.child("username").setValue(dataSnapshot.child("name").getValue());

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        mPDialog.dismiss();

        Intent mainIntent = new Intent(AddNewEvent.this, UpcomingEvent.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();
    }

}

2 个答案:

答案 0 :(得分:0)

如果您只想在对话框已被解除时启动其他活动。在显示对话框之前在updateEvent()内使用此内容:

mPDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    public void onDismiss(DialogInterface dialog) {
        Intent mainIntent = new Intent(AddNewEvent.this, UpcomingEvent.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();
    }
});

mPDialog.show();

然后,只要您的对话框被解除(使用mPDialog.dismiss()或按取消,...),它就会调用onDismiss()。然后是开始新活动的时候了。

答案 1 :(得分:0)

代码中的问题是操作的顺序:执行mPDialog.show();之后,执行mPDialog.dismiss();并开始活动。

您应该显示对话框,然后按下按钮启动活动,如下所示:

AlertDialog mPDialog = new AlertDialog.Builder(this)
    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
        Intent mainIntent = new Intent(AddNewEvent.this, UpcomingEvent.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();)
.show();

或者将用于启动活动的代码放在按钮OnClickListener()中。