I have an app where alarm items are added to my recycler view. They are added to a list and added to a room database. The recycler view updates from the list. When I first add say 3 alarms to the view, then I click on the delete checkbox, and then hit the delete button, it will not work. The view will reset, and then I click the delete checkbox again, and then click delete, it will delete from the recycler view then. Also if say I add some alarms, and then close the app, and reopen it, it will work on the first delete. So, basically, when I first add recyclerview items, they will not update/delete properly until I hit the delete button and recheck them, or I restart the app. They are deleted via boolean in a sql table, they are not updating in the sql database when they are first added like they are supposed to. Also how do I break these files into sepereate ones below.
private lateinit var alarmList: MutableList<Alarm>
private lateinit var deleteAlarmList: MutableList<Alarm>
private lateinit var addAlarmButton: Button
private lateinit var deleteAlarmButton: Button
class MainActivity : AppCompatActivity() {
//private var rvAlarms = findViewById<RecyclerView>(R.id.alarmListRecyclerView)
private lateinit var rvAlarms: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rvAlarms = findViewById(R.id.alarmListRecyclerView)
addAlarmButton = findViewById(R.id.addAlarmButton)
deleteAlarmButton = findViewById(R.id.deleteAlarmButton)
AlarmRepository.initialize(this)
val alarmRepository = AlarmRepository.get()
val alarm5 = Alarm()
// val alarm6 = Alarm()
// val alarm7 = Alarm()
var list: MutableList<Alarm> = mutableListOf()
deleteAlarmList = list
alarmList = alarmRepository.getAlarmList() as MutableList<Alarm>
//list.add(alarm6)
rvSet()
//add alarm button listener
//adds new alarm on click
addAlarmButton.setOnClickListener {
val alarm = Alarm()
alarmList.add(alarm)
rvSet()
alarmRepository.addAlarm(alarm)
}
//delete alarm button listener
//deletes all alarms checked with delete mark
deleteAlarmButton.setOnClickListener {
/* var listIterator = alarmRepository.getAlarmList()
val itr = listIterator.iterator()
while (itr.hasNext()){
var alar = itr.next()
if (alar.deleteCheck){
*/
alarmRepository.deleteAlarms()//fixme possible background thread done after next line
here
alarmList = alarmRepository.getAlarmList() as MutableList<Alarm>
rvSet()
}
}
fun rvSet () {
var alarmAdapter = AlarmAdapter(alarmList)
rvAlarms.adapter = alarmAdapter
rvAlarms.layoutManager = LinearLayoutManager(this)
}
}
////////////////////
class AlarmAdapter(private val alarmList: List<Alarm>) :
RecyclerView.Adapter<AlarmAdapter.AlarmViewHolder>() {
val alarmRepository = AlarmRepository.get()
class AlarmViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val alarmRepository = AlarmRepository.get()
val mondayCheckBox: CheckBox
val deleteCheckBox: CheckBox
val alarmLabel: TextView
init {
mondayCheckBox = view.findViewById(R.id.mondayCheckBox)
deleteCheckBox = view.findViewById(R.id.deleteAlarmCheckBox)
alarmLabel = view.findViewById(R.id.alarmLabel)
mondayCheckBox.setOnClickListener {
alarmLabel.text = "success"
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlarmViewHolder {
return AlarmViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.alarm_list_item, parent, false)
)
}
override fun onBindViewHolder(holder: AlarmViewHolder, position: Int) {
val alarm = alarmList[position]
holder.itemView.apply {
deleteAlarmCheckBox.setOnClickListener {
alarm.deleteCheck = !alarm.deleteCheck
alarmRepository.updateAlarm(alarm)
}
}
}
override fun getItemCount(): Int {
return alarmList.size
}
}
//////////
@Database(entities = [Alarm::class], version = 1, exportSchema = false)
abstract class AlarmDataBase : RoomDatabase() {
//abstract val alarmDataBaseDao: AlarmDatabaseDao
abstract fun alarmDao(): AlarmDatabaseDao
}
////////////
@Dao
interface AlarmDatabaseDao {
@Insert
fun insertAlarm(alarm: Alarm)
@Update
fun updateAlarm(alarm: Alarm)
@Query("SELECT * FROM alarm_database2 ORDER BY hour ASC")
fun getAllAlarms(): List<Alarm>
@Query("DELETE FROM alarm_database2 WHERE deleteCheck = 1")
fun deleteAlarms(): Integer
@Delete
fun deleteAlarm(alarm: Alarm)
}
//////////
import android.content.Context
import androidx.room.Room
import com.example.alarmapp2.Alarm
import com.example.alarmapp2.MainActivity
import java.util.concurrent.Executors
class AlarmRepository private constructor(context: Context) {
val db = Room.databaseBuilder(
context.applicationContext,
AlarmDataBase::class.java, "alarm_database2"
).allowMainThreadQueries().build()
private val alarmDao = db.alarmDao()
private val executor = Executors.newSingleThreadExecutor()
fun getAlarmList(): List<Alarm> = alarmDao.getAllAlarms()
//fun getCrime(id: UUID): LiveData<Alarm?> = alarmDao.ge(id)
fun addAlarm(alarm: Alarm) {
alarmDao.insertAlarm(alarm)
}
fun deleteAlarms() {
//executor.execute {
alarmDao.deleteAlarms()
//}
}
fun updateAlarm(alarm: Alarm) {
alarmDao.updateAlarm(alarm)
}
companion object {
private var INSTANCE: AlarmRepository? = null
fun initialize(context: MainActivity) {
if (INSTANCE == null) {
INSTANCE = AlarmRepository(context)
}
}
fun get(): AlarmRepository {
return INSTANCE
?: throw IllegalStateException("AlarmRepository must be initialized")
}
}
}
CodePudding user response:
I think you have to call notifyItemRemoved(position)
or notifyDataSetChanged()
when item removed.
CodePudding user response:
I think i figured it out. I had to refresh my list with the alarm objects currently in the database. I dont know why exactly. But it works. Here is what i added to the add alarm button.
addAlarmButton.setOnClickListener {
val alarm = Alarm()
alarmRepository.addAlarm(alarm)
alarmList = alarmRepository.getAlarmList() as MutableList<Alarm>
rvSet()
}