在哪里调用finish()?

时间:2016-02-22 13:49:50

标签: android sqlite sql-update activity-finish

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

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

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

1)但是现在,一旦点击保存,活动就会完成。

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

创建表

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

1 个答案:

答案 0 :(得分:1)

如果您使用Snackbar,您可以使用Snackbar.Callback界面在小吃栏被解除时收听,然后完成活动。

示例:

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!