Reputation: 53
I'm trying to get the data that the user is enter in the autocompleteview but i doesnt receive it in the viewModel.
There's the xml file,
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="connectionViewModel"
type="com.example.soccerinfo.connection.ConnectionViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/mail"
style="@style/textStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_your_mail"
android:inputType="textEmailAddress"
android:text="@{connectionViewModel._connectionMailId}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.23000002" />
<Button
android:id="@+id/connectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_in"
android:onClick="@{() -> connectionViewModel.onConnection()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mail" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
There is the viewModel, in the insertUpdate function I'm trying to display the connectionMailId. The connectionMailId display an empty string when I want to display ion the logs
class ConnectionViewModel(
val database: ConnectionDataBaseDao,
private val app : Application) : AndroidViewModel(app){
private val _eventConnectionMade = MutableLiveData<Boolean>()
val eventConnectionMade : LiveData<Boolean>
get() = _eventConnectionMade
val _mails : MutableLiveData<List<Connection>> = MutableLiveData()
var _connectionMailId = String()
init {
viewModelScope.launch(Dispatchers.IO) {
_mails.postValue(database.getAllConnections())
}
_connectionMailId = ""
}
fun insertUpdateConnection(mail: String){
viewModelScope.launch(Dispatchers.IO){
Timber.i("Email binding : $_connectionMailId")
var connection = Connection(mail)
if (requireNotNull(_mails.value?.any { connection -> connection.mailId.equals(mail) })){
update(connection)
}else{
insert(connection)
}
}
}
I hope you will help thanks a lot.
Upvotes: 1
Views: 382
Reputation: 1297
Try making _connectionMailId
a live data of type String
:
var _connectionMailId = MutableLiveData<String>()
also, use two way data binding:
android:text="@={connectionViewModel._connectionMailId}"
Upvotes: 1