Я новичок в android и SQLite, и у меня есть проблема с моим кодом, из-за которого возникает ошибка "нет такого столбца: год в" событии SELECT, время, дата, месяц, год ОТ события события ГДЕ месяц =? и year =? ", когда я пытаюсь запустить свою деятельность.
Я также новичок в StackOverflow, поэтому не стесняйтесь сказать мне, если я сделал что-то не так:)
Спасибо все
Вот мой код моего класса и мой DBopenHelper
public class CustomCalendarView extends LinearLayout {
TextView previousMonth, currentMonth, followMonth, newEvent;
GridView gridView;
private static final int MAX_CALENDAR_DAYS = 35;
Calendar calendar = Calendar.getInstance(Locale.FRENCH);
Context context;
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM",Locale.FRENCH);
SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM", Locale.FRENCH);
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy",Locale.FRENCH);
SimpleDateFormat eventDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.FRENCH);
CalendarAdapter myGridAdapter;
AlertDialog alertDialog;
List<Date> dates = new ArrayList<>();
List<Events> eventsList = new ArrayList<>();
DBOpenHelper dbOpenHelper;
public CustomCalendarView (Context context){super(context);}
public CustomCalendarView (final Context context, @Nullable AttributeSet attrs){
super(context, attrs);
this.context = context;
InitializeLayout();
SetUpCalendar();
previousMonth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.MONTH, -1);
SetUpCalendar();
}
});
followMonth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.MONTH, 1);
SetUpCalendar();
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
final View addView = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_newevent_layout, null);
final EditText EventName = addView.findViewById(R.id.eventname);
final TextView EventTime = addView.findViewById(R.id.eventtime);
ImageButton SetTime = addView.findViewById(R.id.seteventtime);
Button AddEvent = addView.findViewById(R.id.addevent);
SetTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(addView.getContext(), R.style.Theme_AppCompat_Dialog
, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.setTimeZone(TimeZone.getDefault());
SimpleDateFormat format = new SimpleDateFormat("K:mm a", Locale.FRENCH);
String event_Time = format.format(c.getTime());
EventTime.setText(event_Time);
}
}, hours, minutes, false);
timePickerDialog.show();
}
});
final String date = eventDateFormat.format(dates.get(position));
final String month = monthFormat.format(dates.get(position));
final String year = yearFormat.format(dates.get(position));
AddEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SaveEvent(EventName.getText().toString(), EventTime.getText().toString(), date, month, year);
SetUpCalendar();
alertDialog.dismiss();
}
});
builder.setView(addView);
alertDialog = builder.create();
alertDialog.show();
}
});
}
public CustomCalendarView (Context context, @Nullable AttributeSet attrs, int defStyleAttr){
super(context, attrs, defStyleAttr);
}
private void SaveEvent(String event, String time, String date, String month, String year){
dbOpenHelper = new DBOpenHelper(context);
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
dbOpenHelper.SaveEvent(event, time, date, month, year, database);
dbOpenHelper.close();
Toast.makeText(context, "Event Saved", Toast.LENGTH_SHORT).show();
}
private void InitializeLayout (){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.calendar_layout, this);
followMonth = view.findViewById(R.id.next_month);
previousMonth = view.findViewById(R.id.previous_month);
currentMonth = view.findViewById(R.id.current_month);
gridView = view.findViewById(R.id.gridView);
newEvent = view.findViewById(R.id.newevent);
}
private void SetUpCalendar(){
String currentDate = dateFormat.format(calendar.getTime());
currentMonth.setText(currentDate);
dates.clear();
Calendar monthCalendar = (Calendar) calendar.clone();
monthCalendar.set(Calendar.DAY_OF_MONTH,1);
int FirstDayofMonth = monthCalendar.get(Calendar.DAY_OF_WEEK) - 1;
monthCalendar.add(Calendar.DAY_OF_MONTH, -FirstDayofMonth);
CollectEventsPerMonth(monthFormat.format(calendar.getTime()),yearFormat.format(calendar.getTime()));
while (dates.size() < MAX_CALENDAR_DAYS){
dates.add(monthCalendar.getTime());
monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
myGridAdapter = new CalendarAdapter(context, dates, calendar, eventsList);
gridView.setAdapter(myGridAdapter);
}
private void CollectEventsPerMonth (String Month, String Year) {
eventsList.clear();
dbOpenHelper = new DBOpenHelper(context);
SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
Cursor cursor = dbOpenHelper.ReadEventsperMonth(Month, Year, database);
while (cursor.moveToNext()){
String event = cursor.getString(cursor.getColumnIndex(DBStructure.EVENT));
String time = cursor.getString(cursor.getColumnIndex(DBStructure.TIME));
String date = cursor.getString(cursor.getColumnIndex(DBStructure.DATE));
String month = cursor.getString(cursor.getColumnIndex(DBStructure.MONTH));
String year = cursor.getString(cursor.getColumnIndex(DBStructure.YEAR));
Events events = new Events(event, time, date, month, year);
eventsList.add(events);
}
cursor.close();
dbOpenHelper.close();
}
}
publi c класс DBOpenHelper расширяет SQLiteOpenHelper {
private static final String CREATE_EVENTS_TABLE = "create table " + DBStructure.EVENT_TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, "
+ DBStructure.EVENT + " TEXT, " + DBStructure.TIME + " TEXT, " + DBStructure.DATE + " TEXT, " + DBStructure.MONTH + " TEXT, "
+ DBStructure.YEAR + "TEXT)";
private static final String DROP_EVENTS_TABLE = "DROP TABLE IF EXISTS " + DBStructure.EVENT_TABLE_NAME;
public DBOpenHelper(@Nullable Context context) {
super(context, DBStructure.DB_NAME, null, DBStructure.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_EVENTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_EVENTS_TABLE);
onCreate(db);
}
public void SaveEvent (String event, String time, String date, String month, String year, SQLiteDatabase database){
ContentValues contentValues = new ContentValues();
contentValues.put (DBStructure.EVENT, event);
contentValues.put(DBStructure.TIME,time);
contentValues.put(DBStructure.MONTH,month);
contentValues.put(DBStructure.YEAR,year);
database.insert(DBStructure.EVENT_TABLE_NAME, null, contentValues);
}
public Cursor ReadEvents (String date, SQLiteDatabase database){
String [] Projections = {DBStructure.EVENT, DBStructure.TIME, DBStructure.DATE, DBStructure.MONTH, DBStructure.YEAR};
String Selection = DBStructure.DATE +"=?";
String [] SelectionArgs = {date};
return database.query(DBStructure.EVENT_TABLE_NAME, Projections, Selection, SelectionArgs, null,null,null);
}
public Cursor ReadEventsperMonth (String month, String year, SQLiteDatabase database){
String [] Projections = {DBStructure.EVENT, DBStructure.TIME, DBStructure.DATE, DBStructure.MONTH, DBStructure.YEAR};
String Selection = DBStructure.MONTH +"=? and " + DBStructure.YEAR + "=?";
String [] SelectionArgs = {month, year};
return database.query(DBStructure.EVENT_TABLE_NAME, Projections, Selection, SelectionArgs, null,null,null);
}
}