在哪里调用 finish()?

Where to call finish()?

我正在保存一个活动。当我点击保存时,我想检查标题是否不为空,开始时间是否等于结束时间和剂量事件是否重叠,然后保存事件。

我有一个布尔变量来检查编辑模式是否为真。如果是,我想更新现有的活动。

现在我尝试这样做,但是当我保存事件时,我想检查所有这些场景并显示快餐栏,然后完成 activity。

1) 但是现在 activity 一点击保存就完成了。

2) 另外当我想更新时它显示事件存在。

这是怎么回事?

  save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            eventTitle = title.getText().toString();
            location = mAutocompleteView.getText().toString();


            if (eventTitle.length() == 0) {

                showAlert("Title can not be empty.");

            }
            else if (startTime.equals(endTime)) {
                showAlert("End Time can not be same as Start Time.");
            }

            else if (db.doesEventOverlap(startTime,endTime,dayOfWeek)) {

               showAlert("Event Exists");

            }

            else {

                if (editMode) {

                eventData.setTitle(title.getText().toString());
                eventData.setFromDate(fromDate);
                eventData.setToDate(toDate);
                eventData.setDayOfWeek(selectDay.getText().toString());
                eventData.setLocation(mAutocompleteView.getText().toString());
                eventData.setNotificationTime(notificationTime);

                    db.updateEvent(eventData);

                    Toast.makeText(getApplicationContext(),"Event Updated",Toast.LENGTH_SHORT).show();
                }

               else {
                        db.addEvent(new EventData(eventTitle, startTime, endTime, dayOfWeek, location, notificationTime, mColor));
                        showAlert("Event Added.");
                }

                setResult(RESULT_OK, i);
                finish();
            }
        }
    });

剂量事件重叠函数:

     boolean doesEventOverlap(String startTime, String endTime, String day) {
    String selectQuery = "SELECT COUNT(*) FROM " + TABLE + " WHERE " + KEY_DAY_OF_WEEK + " = '" + day + "'" + " AND "

            + "(" + KEY_FROM_DATE + " < '" + startTime
            + "' AND '" + startTime + "' < " + KEY_TO_DATE + ") OR "

            + " (" + KEY_FROM_DATE + " < '" + endTime
            + "' AND '" + endTime + "' < " + KEY_TO_DATE + ") OR "

            + " (" + KEY_FROM_DATE + " < '" + startTime
            + "' AND '" + endTime + "' < " + KEY_TO_DATE + ") OR "

            + " ('" + startTime + "' < " + KEY_FROM_DATE
            + " AND " + KEY_TO_DATE + " < '" + endTime + "')";


    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            EventData event = new EventData();
            return cursor.getInt(0) > 0;
        } while (cursor.moveToNext());
    }
    return false;
}

创建table

  public void createTable(SQLiteDatabase db){
    String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_TITLE + " TEXT,"
            + KEY_FROM_DATE + " DATE,"
            + KEY_TO_DATE + " DATE,"
            + KEY_DAY_OF_WEEK + " TEXT,"
            + KEY_LOCATION + " TEXT,"
            + KEY_NOTIFICATION_TIME + " DATE,"
            + KEY_COLOR + " TEXT" + ")";

    db.execSQL(CREATE_EVENTS_TABLE);
}

谢谢..

编辑:

显示小吃店的警报方法。

 public void showAlert(String alert){

    Snackbar.make(parentLayout, alert, Snackbar.LENGTH_LONG).show(); // Don’t forget to show!

    Snackbar.setCallback(new Snackbar.Callback() {
        @Override
        public void onDismissed(Snackbar snackbar, int event) {
            super.onDismissed(snackbar, event);
            finish();
        }

        @Override
        public void onShown(Snackbar snackbar) {
            super.onShown(snackbar);
        }
    }
}

如果你使用的是Snackbar,可以在snackbar dismissed时使用Snackbar.Callback接口监听,然后完成activity。

示例:

Snackbar snackbar = Snackbar.make(save, "Message", Snackbar.LENGTH_SHORT);
        snackbar.setCallback(new Snackbar.Callback() {
            @Override
            public void onDismissed(Snackbar snackbar, int event) {
                super.onDismissed(snackbar, event);
                finish();
            }

            @Override
            public void onShown(Snackbar snackbar) {
                super.onShown(snackbar);
            }
        });

已编辑:

Snackbar snackbar = Snackbar.make(parentLayout, alert, Snackbar.LENGTH_LONG);

        snackbar.setCallback(new Snackbar.Callback() {
            @Override
            public void onDismissed(Snackbar snackbar, int event) {
                super.onDismissed(snackbar, event);
                finish();
            }

            @Override
            public void onShown(Snackbar snackbar) {
                super.onShown(snackbar);
            }
        });

        snackbar.show(); // Don’t forget to show!