我想将所选项目保存在日历中。重新启动后,将它们重置。我如何实施他们的发明? example selecting 也许我应该使用其他库或数据库?
这是我尝试编写的数据库:
MainActivity.kt:
AttributeError
DBHelper.kt:
class HomeActivity : AppCompatActivity() {
var dbHelper: DBHelper? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.exmaple_1_fragment)
val daysOfWeek = daysOfWeekFromLocale()
legendLayout.children.forEachIndexed { index, view ->
(view as TextView).apply {
text =
daysOfWeek[index].getDisplayName(TextStyle.SHORT, Locale.ENGLISH).toUpperCase(
Locale.ENGLISH
)
setTextColorRes(R.color.example_1_white_light)
}
}
dbHelper = DBHelper(this, name = "main", version = 1)
val currentMonth = YearMonth.now()
val startMonth = currentMonth.minusMonths(10)
val endMonth = currentMonth.plusMonths(10)
exOneCalendar.setup(startMonth, endMonth, daysOfWeek.first())
exOneCalendar.scrollToMonth(currentMonth)
class DayViewContainer(view: View) : ViewContainer(view) {
// Will be set when this container is bound. See the dayBinder.
lateinit var day: CalendarDay
val textView = view.exOneDayText
val database = dbHelper!!.writableDatabase
val contentValues = ContentValues()
init {
view.setOnClickListener {
if (day.owner == DayOwner.THIS_MONTH) {
if (selectedDates.contains(day.date)) {
selectedDates.remove(day.date)
contentValues.put(DBHelper.DATABASE_DAY, selectedDates.contains(day.date))
database.insert(DBHelper.TABLE_DATA, null, contentValues)
} else {
selectedDates.add(day.date)
database.delete(DBHelper.TABLE_DATA, null, null)
}
exOneCalendar.notifyDayChanged(day)
}
}
}
}
答案 0 :(得分:0)
DBHelper
class DBHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION){
companion object {
const val DATABASE_VERSION = 1
const val DATABASE_NAME = "DateManager.db"
const val TABLE_DATE = "date"
const val COLUMN_USER_ID = "user_id"
const val COLUMN_DATES = "dates"
}
private val DROP_USER_TABLE = "DROP TABLE IF EXISTS $TABLE_DATE"
private val CREATE_DATA_TABLE = ("CREATE TABLE " + TABLE_DATE + "( "
+ COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DATES + " TEXT" + ")")
override fun onCreate(db: SQLiteDatabase) {
db!!.execSQL(CREATE_DATA_TABLE)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL(DROP_USER_TABLE)
onCreate(db)
}
fun getAllDates(): String {
var allUser: String = ""
val db = readableDatabase
val selectALLQuery = "SELECT * FROM $TABLE_DATE"
val cursor = db.rawQuery(selectALLQuery, null)
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
var date_read = cursor.getString(cursor.getColumnIndex(COLUMN_DATES))
allUser = "$allUser\n$date_read"
} while (cursor.moveToNext())
}
}
cursor.close()
db.close()
return allUser
}
fun deleteUser(model: DBmodel) {
val db = this.writableDatabase
db.delete(TABLE_DATE, "$COLUMN_DATES = '${model.date}'", null)
db.close()
}
fun addUser(model: DBmodel):Long{
val database = this.writableDatabase
val values = ContentValues()
values.put(COLUMN_DATES, model.date)
val success = database.insert(TABLE_DATE, null , values)
database.close()
return success
}
}
MainActivity.kt
class HomeActivity : AppCompatActivity() {
private val activity = this@HomeActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.exmaple_1_fragment)
val daysOfWeek = daysOfWeekFromLocale()
database = DBHelper(this)
legendLayout.children.forEachIndexed { index, view ->
(view as TextView).apply {
text =
daysOfWeek[index].getDisplayName(TextStyle.SHORT, Locale.ENGLISH).toUpperCase(
Locale.ENGLISH
)
setTextColorRes(R.color.example_1_white_light)
}
}
val currentMonth = YearMonth.now()
val startMonth = currentMonth.minusMonths(10)
val endMonth = currentMonth.plusMonths(10)
exOneCalendar.setup(startMonth, endMonth, daysOfWeek.first())
exOneCalendar.scrollToMonth(currentMonth)
class DayViewContainer(view: View) : ViewContainer(view) {
lateinit var day: CalendarDay
val textView = view.exOneDayText
init {
view.setOnClickListener {
if (day.owner == DayOwner.THIS_MONTH) {
if (selectedDates.contains(day.date)) {
selectedDates.remove(day.date)
Toast.makeText(this@HomeActivity, "("+day.date.toString().trim()+") deleted", Toast.LENGTH_SHORT).show()
val user = DBmodel(date = day.date.toString().trim())
database!!.deleteUser(user)
} else {
selectedDates.add(day.date)
val user = DBmodel(date = day.date.toString().trim())
database!!.addUser(user)
Toast.makeText(this@HomeActivity, "("+day.date.toString().trim()+") added", Toast.LENGTH_SHORT).show()
}
exOneCalendar.notifyDayChanged(day)
}
}
}
}
exOneCalendar.dayBinder = object : DayBinder<DayViewContainer> {
override fun create(view: View) = DayViewContainer(view)
override fun bind(container: DayViewContainer, day: CalendarDay)
{
container.day = day
val textView = container.textView
val db = database!!.readableDatabase
var allUser: String = ""
val selectALLQuery = "SELECT * FROM ${DBHelper.TABLE_DATE}"
val cursor = db.rawQuery(selectALLQuery, null)
textView.text = day.date.dayOfMonth.toString()
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
var date_read =
cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DATES))
when {
day.date.toString() == date_read -> {
selectedDates.add(day.date)
}
}
} while (cursor.moveToNext())
}
}
cursor.close()
db.close()
if (day.owner == DayOwner.THIS_MONTH) {
when {
selectedDates.contains(day.date) -> {
textView.setTextColorRes(R.color.example_1_bg)
textView.setBackgroundResource(R.drawable.example_1_selected_bg)
}
today == day.date -> {
textView.setTextColorRes(R.color.example_1_white)
textView.setBackgroundResource(R.drawable.example_1_today_bg)
}
else -> {
textView.setTextColorRes(R.color.example_1_white)
textView.background = null
}
}
} else {
textView.setTextColorRes(R.color.example_1_white_light)
textView.background = null
}
}
}
数据库模型
data class DBmodel(val id: Int = -1, val date: String)