我正在开发一款应用。将打开活动,询问“标题”,“描述”,“使用Google API地点选择器的地点”,“使用日期和时间选择器的日期和时间”,然后单击“创建”按钮会导致数据保存在数据库中。
问题是当用户从日期选择器选择的当月日期工作正常并存储在DB中但是当用户选择下个月的日期时,它不会在Db中存储该条目的数据。
Create.java
package com.example.akshay.eventmanager;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.model.LatLng;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import android.app.DatePickerDialog.OnDateSetListener;
/**
* Created by Akshay on 7/12/2015.
*/
public class Create extends Activity implements View.OnClickListener {
Button create;
EditText title, desc, time, Venue;
TextView venueInfo;
PlacePicker.IntentBuilder intentBuilder;
private static final int PLACE_PICKER_REQUEST = 1;
public LatLng gotLatLng;
String placeName;
String plName;
String Addr;
Double gotLat, gotLong;
String LAT, LONG;
DataBaseHelper myDB;
public static final String LOG_TAG = "Create";
String GotTime = " ";
String GotDate = " ";
DatePickerDialog datePicker;
TimePickerDialog myTimePicker;
SimpleDateFormat dateFormatter;
String TimeForDB;
String DateForDB;
int DayOfMonth;
int MonthName;
static boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
dateFormatter = new SimpleDateFormat("EEEE dd", Locale.US);
showDate();
showTime();
casting();
}
public void casting() {
create = (Button) findViewById(R.id.bCreate);
Venue = (EditText) findViewById(R.id.etVenue);
title = (EditText) findViewById(R.id.etTitle);
desc = (EditText) findViewById(R.id.etDescription);
time = (EditText) findViewById(R.id.etTime);
time.setInputType(InputType.TYPE_NULL);
Venue.setOnClickListener(this);
time.setOnClickListener(this);
create.setOnClickListener(this);
Venue.setInputType(InputType.TYPE_NULL);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.etVenue:
try {
intentBuilder = new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(Create.this);
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
Toast.makeText(Create.this, "Google Play Services is not available.",
Toast.LENGTH_LONG)
.show();
}
break;
case R.id.bCreate:
String TITLE = title.getText().toString();
String DESC = desc.getText().toString();
String eventTime = time.getText().toString();
String PLACE = Venue.getText().toString();
myDB = new DataBaseHelper(this);
int flag = myDB.InsertValues(TITLE, DESC, PLACE, LAT, LONG, Addr, DateForDB, TimeForDB, DayOfMonth, MonthName);
if (flag == 1) {
Log.e(LOG_TAG , TITLE);
Log.e(LOG_TAG , DESC);
Log.e(LOG_TAG , PLACE);
Log.e(LOG_TAG , LAT);
Log.e(LOG_TAG , LONG);
Log.e(LOG_TAG , Addr);
Log.e(LOG_TAG , DateForDB);
Log.e(LOG_TAG , TimeForDB);
Log.e(LOG_TAG , String.valueOf(DayOfMonth));
Log.e(LOG_TAG , String.valueOf(MonthName));
Toast.makeText(Create.this, String.valueOf(DayOfMonth), Toast.LENGTH_LONG).show();
Toast.makeText(Create.this, String.valueOf(MonthName), Toast.LENGTH_LONG).show();
Toast.makeText(Create.this, "Sucess", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Create.this, "Error Adding Records", Toast.LENGTH_LONG).show();
}
break;
case R.id.etTime:
myTimePicker.show();
datePicker.show();
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
gotLatLng = place.getLatLng();
plName = (String) place.getName();
Addr = (String) place.getAddress();
gotLat = gotLatLng.latitude;
gotLong = gotLatLng.longitude;
LAT = String.valueOf(gotLat);
LONG = String.valueOf(gotLong);
placeName = String.format("Place: %s", place.getName());
Toast.makeText(this, placeName, Toast.LENGTH_LONG).show();
Venue.setText(plName);
}
}
}
public void showDate() {
Calendar cal = Calendar.getInstance();
datePicker = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
DayOfMonth = dayOfMonth;
MonthName = monthOfYear;
MonthName = MonthName+1;
monthOfYear = monthOfYear+1;
DateForDB = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear) + "/" + String.valueOf(year);
GotDate = dateFormatter.format(newDate.getTime()).toString();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_WEEK));
flag = true;
}
public void showTime() {
if (flag == true) {
final Calendar calender = Calendar.getInstance();
myTimePicker = new TimePickerDialog(this, new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar newTime = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hourOfDay);
calender.set(Calendar.MINUTE, minute);
TimeForDB = String.valueOf(hourOfDay) + " : " + String.valueOf(minute);
GotTime = GotDate + " At " + String.valueOf(hourOfDay) + ":" + String.valueOf(minute);
time.setText(GotTime);
}
}, calender.get((Calendar.HOUR_OF_DAY)), calender.get(Calendar.MINUTE), true);
}
}
}
DataBaseHelper.java
package com.example.akshay.eventmanager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import java.sql.SQLException;
import java.util.Calendar;
/**
* Created by Akshay on 7/12/2015.
*/
public class DataBaseHelper extends SQLiteOpenHelper {
static final String DB_NAME = "MyEvents.db";
static final String TABLE_NAME = "MyEvents";
public static final String COL_1 = "ID";
public static final String COL_2 = "Title";
public static final String COL_3 = "Description";
public static final String COL_4 = "Venue";
public static final String COL_5 = "Latitude";
public static final String COL_6 = "Longitude";
public static final String COL_7 = "Address";
public static final String COL_8 = "Date";
public static final String COL_9 = "Time";
public static final String COL_10 = "Day_Of_Month";
public static final String COL_11 = "Month";
SQLiteDatabase myDB;
int date, month;
Calendar c;
final static String Log_Tag = "CheckDataBase";
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "(" + COL_1 + " INTEGER PRIMARY KEY," + COL_2 + " TEXT," + COL_3 + " TEXT,"
+ COL_4 + " TEXT," + COL_5 + " TEXT," + COL_6 + " TEXT," + COL_7 + " TEXT," + COL_8 + " TEXT," + COL_9 + " TEXT," + COL_10 + " INTEGER ," + COL_11 + " INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop Table If Exist " + TABLE_NAME);
onCreate(db);
}
public Integer InsertValues(String title, String Desc, String Venue, String Lat, String Long, String Addr, String Date, String Time, int DayOfMonth, int MonthName)
{
myDB = this.getWritableDatabase();
ContentValues cV = new ContentValues();
cV.put(COL_2, title);
cV.put(COL_3, Desc);
cV.put(COL_4, Venue);
cV.put(COL_5, Lat);
cV.put(COL_6, Long);
cV.put(COL_7, Addr);
cV.put(COL_8, Date);
cV.put(COL_9, Time);
cV.put(COL_10, DayOfMonth);
cV.put(COL_11, MonthName);
long isInserted = myDB.insert(TABLE_NAME, null, cV);
if (isInserted == -1) {
return 0;
} else {
return 1;
}
}
public Cursor getData() {
c = Calendar.getInstance();
date = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH);
Log.e(Log_Tag, String.valueOf(date));
Log.e(Log_Tag, String.valueOf(month+1));
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * From " + TABLE_NAME + " Where " + COL_10 + ">" + date + " AND " + COL_11 + ">=" + month, null);
return res;
}
public Cursor getDataExpired() {
c = Calendar.getInstance();
date = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH);
Log.e(Log_Tag, String.valueOf(date));
Log.e(Log_Tag, String.valueOf(month+1));
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * From " + TABLE_NAME + " Where " + COL_10 + "<" + date + " AND " + COL_11 + "<=" + month, null);
return res;
}
}
我使用Log.e打印的值工作正常。