RecyclerView IndexOutOfBoundsException при удалении записи из базы данных при удалении записи из базы данных - PullRequest
0 голосов
/ 20 января 2020

Здравствуйте, я создаю простое приложение с recyclerView. Смотрит "Напоминание". В этом коде, если я сначала прокручиваю все, все хорошо - без исключений, но если я снова прокручиваю, нет ... Этот метод удаления напоминает, какое время после календарного времени

class RecyclerViewAdapter(
var context: Context,
var db: SQLiteDatabase,
var plans: ArrayList<Plan>

): RecyclerView. Adapter () {var can = true переопределить fun onCreateViewHolder (parent: ViewGroup, viewType: Int): MyViewHolder {val view = LayoutInflater.from (parent.context) .inflate (R.layout.one_plan_rv, parent, false) вернуть MyViewHolder ( view)}

override fun getItemCount(): Int {
    val cursor = db.query(TableInfo.TABLENAME, null, null, null, null, null, null)
    val how = cursor.count
    cursor.close()
    return how
}

@SuppressLint("ResourceAsColor")
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

    val title = holder.view.rvTitle
    val plan = holder.view.rvPlan
    val time = holder.view.rvHour
    val date = holder.view.rvDate
    val llPlan = holder.itemView.bgLL


    val currentC = Calendar.getInstance()
    val currentYear = currentC.get(Calendar.YEAR)
    val currentMonth = currentC.get(Calendar.MONTH)
    val currentDay = currentC.get(Calendar.DAY_OF_MONTH)
    val currentHour = currentC.get(Calendar.HOUR)
    val currentMinute = currentC.get(Calendar.MINUTE)
    val currentTime =
        (Calendar.Builder().setDate(currentYear, currentMonth, currentDay)
            .setTimeOfDay(currentHour, currentMinute, 0).build()).timeInMillis


    val inputDate = plans[holder.adapterPosition].date.toString()
    val inputYear = (inputDate[6] + "" + inputDate[7] + inputDate[8] + inputDate[9])
    val inputMonth = (inputDate[3] + "" + inputDate[4])
    val inputDay = (inputDate[0] + "" + inputDate[1])

    val inputClock = plans[holder.adapterPosition].time.toString()
    val inputHour = inputClock[0] + "" + inputClock[1]
    val inputMinute = inputClock[3] + "" + inputClock[4]


    val inputTime = (Calendar.Builder()
        .setDate(inputYear.toInt(), inputMonth.toInt() - 1, inputDay.toInt())
        .setTimeOfDay(inputHour.toInt(), inputMinute.toInt(), 0).build()).timeInMillis

    if (inputTime <= currentTime) {
        var cursor = db.query(TableInfo.TABLENAME, null, null, null, null, null, null)
        if (can) {
            db.delete(
                TableInfo.TABLENAME,
                BaseColumns._ID + "=?",
                arrayOf(plans[holder.adapterPosition].id.toString())
            )
            plans.removeAt(holder.adapterPosition)
            can = false
        }

    }
    else
    {

        title.text = plans[holder.adapterPosition].title
        plan.text = plans[holder.adapterPosition].plan
        time.text = plans[holder.adapterPosition].time
        date.text = plans[holder.adapterPosition].date


        when (plans[holder.adapterPosition].importance) {
            "Low" -> llPlan.setBackgroundResource(R.color.green)
            "Medium" -> llPlan.setBackgroundResource(R.color.yellow)
            "High" -> llPlan.setBackgroundResource(R.color.red)
            else -> llPlan.setBackgroundResource(R.color.red)
        }
    }
}

}

class MyViewHolder (var view: View): RecyclerView.ViewHolder (view)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...