I'm trying to read all the occurs of a table from a Fragment, after an insert. The query read all occurs except the last record previosly inserted . The insert was in one fragment, the select was in another fragment.
Read a table dbroom with all occur.
[SchedaDAO]
@Dao
interface SchedaDAO {
@Insert
fun insert(scheda: Scheda)
@Query(value = "INSERT INTO SCHEDA_TABLE(nome,ripetizioni,id_esercizio, riposo) VALUES (:nome, :ripetizioni, :idesercizio, :riposo)")
fun insert(nome : String, riposo : String, idesercizio : Long, ripetizioni : String)
@Update
fun update(scheda: Scheda)
@Delete
fun delete(scheda: Scheda)
@Query(value = "Delete from scheda_table ")
fun deleteAllScheda ()
@Query("DELETE FROM scheda_table WHERE id = :id")
fun deleteByIdScheda(id: Long)
@Query("select * FROM scheda_table WHERE id = :id")
fun selectByIdScheda(id: Long) : Scheda
@Query("Select * from scheda_table ORDER BY nome ASC")
fun getALL() : List<Scheda>
}
[EserciziDatabase]
@Database (entities = [Esercizi::class,Scheda::class], version = 3 , exportSchema = false)
abstract class EserciziDatabase : RoomDatabase() {
abstract val eserciziDao : EserciziDAO
abstract val schedaDAO : SchedaDAO
companion object {
@Volatile
private var INSTANCE : EserciziDatabase? = null
fun getInstance (context: Context) : EserciziDatabase {
synchronized(this) {
var instance = INSTANCE
if (instance == null) {
instance = Room.databaseBuilder( context.applicationContext, EserciziDatabase::class.java,
"esercizi_database")
.allowMainThreadQueries()
.createFromAsset("database/esercizi_database.db")
.build()
INSTANCE = instance
}
return instance
}
}
}
}
[EserciziViewModel ]
class EserciziViewModel (private val repository: EserciziRepository) : ViewModel() {
var idesercizio: Int = 0
fun setidesercizio(str_id_esercizio: Int) {
idesercizio = str_id_esercizio
}
fun getidesercizio() = idesercizio
suspend fun myAllEsercizi() : List<Esercizi> = repository.myAllEsercizi()
suspend fun insert(esercizi: Esercizi) = viewModelScope.launch(Dispatchers.IO) {
try {
repository.insert(esercizi)
} catch (e : Exception ) {
Log.e(" errore generico:","valore = ${e}")
throw java.lang.IllegalStateException("non mi piace questo errore")
}
}
suspend fun update(esercizi: Esercizi) = viewModelScope.launch(Dispatchers.IO) {
repository.update(esercizi)
}
fun delete(esercizi: Esercizi) = viewModelScope.launch(Dispatchers.IO) {
repository.delete(esercizi)
}
fun deleteByIdEsercizio(id :Long) = viewModelScope.launch(Dispatchers.IO) {
repository.deleteByIdEsercizio(id)
}
suspend fun deleteAllEsercizi() = viewModelScope.launch(Dispatchers.IO) {
repository.deleteAllEsercizi()
}
private val _list = MutableLiveData<List<Scheda>>().apply {
value = repository.myAllScheda
}
val list: LiveData<List<Scheda>> = _list
//val myAllScheda : LiveData<List<Scheda>> = repository.myAllScheda
suspend fun insert(scheda: Scheda) = viewModelScope.launch(Dispatchers.IO) {
repository.insert(scheda)
}
fun insertScheda(nome : String, ripetizioni: String , idesercizio : Long, riposo : String) = viewModelScope.launch(Dispatchers.IO) {
repository.insertScheda(nome, ripetizioni, idesercizio, riposo)
}
suspend fun update(scheda: Scheda) = viewModelScope.launch(Dispatchers.IO) {
repository.update(scheda)
}
fun delete(scheda: Scheda) = viewModelScope.launch(Dispatchers.IO) {
repository.insert(scheda)
}
suspend fun deleteByIdScheda(id :Long) = viewModelScope.launch(Dispatchers.IO) {
repository.deleteByIdScheda(id)
}
suspend fun deleteAllScheda() = viewModelScope.launch(Dispatchers.IO) {
repository.deleteAllScheda()
}
val _muscolo = MutableLiveData<String>()
fun setMuscolo(str_muscolo: String) {
_muscolo.value = str_muscolo
}
fun getmuscolo() = _muscolo
var esercizio: String = ""
var ripetizioni: Int = 0
var riposo: Int = 0
fun setesercizio(str_esercizio: String) {
esercizio = str_esercizio
}
fun setripetizioni(str_ripetizioni: Int) {
ripetizioni = str_ripetizioni
}
fun setriposo(str_riposo: Int) {
riposo = str_riposo
}
fun getesercizio() = esercizio
fun getripetizioni() = ripetizioni
fun getriposo() = riposo
}
class EserciziViewModelFactory (private var repository: EserciziRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create (modelClass: Class<T>) : T {
if (modelClass.isAssignableFrom(EserciziViewModel::class.java)){
return EserciziViewModel(repository) as T
}else {
throw java.lang.IllegalArgumentException("unkwnon view model")
}
}
}
[EserciziRepository ]
class EserciziRepository (private val eserciziDAO: EserciziDAO , private val schedaDAO: SchedaDAO) {
suspend fun myAllEsercizi () : List<Esercizi> = eserciziDAO.getALLEsercizi()
@WorkerThread
suspend fun insert (esercizi: Esercizi) {
eserciziDAO.insert(Esercizi())
}
@WorkerThread
suspend fun update (esercizi: Esercizi) {
eserciziDAO.update(Esercizi())
}
@WorkerThread
suspend fun delete (esercizi: Esercizi) {
eserciziDAO.delete(Esercizi())
}
@WorkerThread
suspend fun deleteAllEsercizi () {
eserciziDAO.deleteAllEsercizi()
}
@WorkerThread
suspend fun deleteByIdEsercizio (id: Long) {
eserciziDAO.deleteByIdEsercizio(id)
}
//@WorkerThread
//fun myAllscheda() : List<Scheda> {
// return schedaDAO.getALL()
//}
val myAllScheda : List<Scheda> = schedaDAO.getALL()
@WorkerThread
suspend fun insert (scheda: Scheda) {
try {
schedaDAO.insert(Scheda())
} catch (e : android.database.SQLException ) {
Log.e(" repository insert:","valore = ${e}")
Log.e(" repository insert:","valore = ${e.localizedMessage}")
throw IllegalStateException("non mi piace questo errore")
}
}
@WorkerThread
suspend fun update (scheda: Scheda) {
schedaDAO.update(Scheda())
}
@WorkerThread
suspend fun delete (scheda: Scheda) {
schedaDAO.delete(Scheda())
}
@WorkerThread
suspend fun deleteAllScheda () {
schedaDAO.deleteAllScheda()
}
@WorkerThread
suspend fun deleteByIdScheda (id: Long) {
schedaDAO.deleteByIdScheda(id)
}
@WorkerThread
suspend fun insertScheda (nome: String, ripetizioni: String, idesercizio : Long, riposo : String) {
schedaDAO.insert(nome, ripetizioni, idesercizio , riposo )
}
}
[SchedaFragment ]
class SchedaFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val ripetizioni : String = eserciziViewModel!!.getripetizioni().toString()
val riposo : String = eserciziViewModel!!.getriposo().toString()
val idesercizio : Int = eserciziViewModel!!.getidesercizio().toString().toInt()
val button = view?.findViewById(R.id.inserisci_text_button) as Button
val nome_scheda = view?.findViewById(R.id.nomeScheda) as TextView
button.setOnClickListener {
lifecycleScope.launch {
insert(nome_scheda.text.toString(), ripetizioni, riposo, idesercizio)// onResult is called on the main thread
}
}
scheda = eserciziViewModel.list
Log.i(" vedo dopo insert :","valore = ${scheda.value}")
}
suspend fun insert(str_nome_sch : String, str_ripetizioni : String, str_riposo : String, str_id_esercizio : Int){
try {
// eserciziViewModel!!.insert(Scheda(str_nome_sch,str_ripetizioni,str_id_esercizio.toLong(),str_riposo))
eserciziViewModel!!.insertScheda(str_nome_sch,str_ripetizioni,str_id_esercizio.toLong(),str_riposo)
} catch (e : Exception ) {
Log.e(" errore insert :","valore = ${e}")
Toast.makeText(requireContext(), " errore inserimento", Toast.LENGTH_SHORT).show()
}finally {
Log.i(" occur inserita :","valore = ${str_nome_sch}")
Toast.makeText(requireContext(), " scheda inserita", Toast.LENGTH_SHORT).show()
}
}
}
[Fragment_third]
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
// Inflate the layout for this fragment
utility.pulisciArray(nome)
val view : View = inflater.inflate(R.layout.fragment_third, container, false)
recyclerView = view.findViewById(R.id.recyclerviewS)
Log.i(" oncreate view :","valore = ${view}")
Log.i(" third fragment :","valore = ${view}")
val data: MutableList<HashMap<String, String>> = mutableListOf()
scheda = eserciziViewModel.list
// legge DB
//eserciziViewModel.list.observe(viewLifecycleOwner, Observer {
eserciziViewModel.list.observe(viewLifecycleOwner, Observer {scheda
Log.i(" scheda variATA:","valore = ${scheda.value}")
scheda.let { Log.i("scheda variata :","valore = ${it}")
}
})