hello community . i have a problem with my code and i can't solve it .
let just a brief of what i'm going to do : i have a address in the ChooseFragment and there is a edit button for editting the address . ok so far good . the edit click is pass new address to the DatabaseRoom and the address text would be changed . but this is happens just for first time . the secound time the address not changed !! . i know that the insert method work and send the new data to the database room but when i want get it with my query (SELECT * FROM ... ) just show the first parameter and not replace with new value . what is wrong with my codes ?
this is my table :
<pre lang="kotlin">@Entity (tableName = "addresstable")
data class AddressTb(
@PrimaryKey(autoGenerate = true)
val id : Int? ,
var address: String)
this is my database :
@Database(entities = [RoomTables::class , AddressTb::class], version = 1, exportSchema = false)
abstract class DataBaseRoom : RoomDatabase() {
abstract fun GetDao(): DaoCart
companion object {
@Volatile
private var instance: DataBaseRoom? = null
private val lock = Any()
operator fun invoke(context: Context) = instance
?: synchronized(lock) {
instance
?: makeDatabase(
context
).also {
instance = it
}
}
private fun makeDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
DataBaseRoom::class.java,
"name"
).build()
}
}
this is my Dao :
<pre>
@Query("SELECT * FROM addresstable")
fun getalladress () : LiveData<AddressTb>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertaddress (model : AddressTb)
@Query("DELETE FROM addresstable")
suspend fun deleteaddress ()
this is my Repository :
fun getalladdress() = db.GetDao().getalladress()
suspend fun deleteaddress() = db.GetDao().deleteaddress()
suspend fun insertaddress(model : AddressTb) = db.GetDao().insertaddress(model)
this is my Viewmodel :
fun getaddress() = repository.getalladdress()
fun deleteaddres() = CoroutineScope(Dispatchers.Default).launch {
repository.deleteaddress()
}
fun insertaddress(model : AddressTb) = CoroutineScope(Dispatchers.Default).launch {
repository.insertaddress(model)
this is my fragment where i fetch the new insert :
class ChosseAddress : Fragment() {
lateinit var viewModelRoom: ViewModelRoom
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val vl = inflater.inflate(R.layout.choose_address_layout, container, false)
val database = DataBaseRoom(requireContext())
val repository = RepositoryCart(database)
val factoryRoom = FactoryRoom(repository)
viewModelRoom =
ViewModelProvider(ViewModelStoreOwner { ViewModelStore() }, factoryRoom).get(
ViewModelRoom::class.java
)
viewModelRoom.getaddress().observe(viewLifecycleOwner, Observer {
try {
vl.txt_address.text = it.address
} catch (ex: Exception) {
null
}
})
val animsec: Animation =
AnimationUtils.loadAnimation(vl.context, R.anim.anim_for_btn_zoom_out)
vl.button_back_choose_address.setOnClickListener {
it.startAnimation(animsec)
childFragmentManager.beginTransaction()
.replace(R.id.choose_address_container, HamburgerFragment())
.commit()
}
vl.edit_address.setOnClickListener {
val mycustomview =
LayoutInflater.from(context).inflate(R.layout.alertfialog_costume, null)
val dialogtext = LayoutInflater.from(context).inflate(R.layout.edit_alert_txt, null)
val mBuilder = AlertDialog.Builder(context)
.setView(mycustomview)
.setCustomTitle(dialogtext)
val show = mBuilder.show()
mycustomview.edit_manually.setOnClickListener {
show.dismiss()
childFragmentManager.beginTransaction()
.replace(R.id.choose_address_container, ManuallyAddressFragment())
.commit()
}
}
return vl
}
}
and this is where i insert data to database :
class ManuallyAddressFragment : Fragment() {
lateinit var viewmodel: ViewModelRoom
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val layoutview = inflater.inflate(R.layout.manually_address_fragment, container, false)
val database = DataBaseRoom(layoutview.context)
val repos = RepositoryCart(database)
val factory = FactoryRoom(repos)
viewmodel = ViewModelProvider(ViewModelStoreOwner { viewModelStore },
factory).get(ViewModelRoom::class.java)
val btncancle: Button = layoutview.btn_cancle
val btnsubmit: Button = layoutview.btn_submit_address
btnsubmit.setOnClickListener {
val edittext = layoutview.edit_address_manually
if (edittext.text.toString().isEmpty()) {
Toast.makeText(context, R.string.submit_btn, Toast.LENGTH_SHORT).show()
} else {
val insert = (AddressTb( null , edittext.text.toString()))
viewmodel.insertaddress(insert)
childFragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.anim_fragment_manually,
R.anim.anim_fragment_chooseaddress
)
.replace(R.id.manually_container, ChosseAddress())
.commit()
Toast.makeText(context, "آدرس شما با موفقیت ثبت شد", Toast.LENGTH_SHORT).show()
}
}
btncancle.setOnClickListener {
childFragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.anim_fragment_manually,
R.anim.anim_fragment_chooseaddress
)
.replace(R.id.manually_container, ChosseAddress())
.commit()
}
return layoutview
}
}
What I have tried:
so i tried so things to solve this but can't figure it out what is wrong ...
i also tried update method instead of my insert method but also not worked .