I have implemented one ViewModel
but I need to change the values of some properties based on their name, more exactly add one
FormViewModel
class FormViewModel : ViewModel(), Observable {
...
var c2=""
@Bindable get
set(v){
field=v
notifyPropertyChanged(BR.c2)
}
var c3=""
@Bindable get
set(v){
field=v
notifyPropertyChanged(BR.c3)
}
var c4=""
@Bindable get
set(v){
field=v
notifyPropertyChanged(BR.c4)
}
var c5=""
@Bindable get
set(v){
field=v
notifyPropertyChanged(BR.c5)
}
var c6=""
@Bindable get
set(v){
field=v
notifyPropertyChanged(BR.c6)
}
...
fun onMore(num:Int){
this["c"+num]=this["c"+num].toInt()+1//esto no sirve pero es lo que quiero
}
}
How could I do this? I have read something about class delegation but I don't know how to implement it in aViewModel
Reason
I have two buttons for each of the fields and I just want to assign a click function to all of them
they must add or subtract 1 according to the button they press
capture_fragment
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/contpor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:contentDescription="Portadores"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/contpdc">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/textC2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/textC3"
app:layout_constraintTop_toTopOf="@id/textC3"
app:layout_constraintWidth_percent="0.3">
<TextView
android:id="@+id/labc2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{@string/c(`2`)}"
android:labelFor="@id/editc2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<EditText
android:id="@+id/editc2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:selectAllOnFocus="true"
android:text="@={modelo.c2}"
android:background="#eee"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintTop_toBottomOf="@id/labc2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/morec2" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/morec2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#00E676"
android:src="@drawable/ic_more"
android:paddingHorizontal="8dp"
android:onClick="@{()->modelo.onMore()}"
app:layout_constraintLeft_toRightOf="@id/editc2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/lessc2"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/lessc2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="@drawable/ic_less"
android:background="#FF6E40"
android:paddingHorizontal="8dp"
app:layout_constraintLeft_toRightOf="@id/editc2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/morec2"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/textC3"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/textC5"
app:layout_constraintLeft_toRightOf="@id/textC2"
app:layout_constraintRight_toLeftOf="@id/textC4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3">
<TextView
android:id="@+id/labc3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{@string/c(`3`)}"
android:labelFor="@id/editc3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<EditText
android:id="@+id/editc3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:selectAllOnFocus="true"
android:text="@={modelo.c3}"
android:background="#eee"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintTop_toBottomOf="@id/labc3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/morec3" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/morec3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#00E676"
android:src="@drawable/ic_more"
android:paddingHorizontal="8dp"
app:layout_constraintLeft_toRightOf="@id/editc3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/lessc3"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/lessc3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="@drawable/ic_less"
android:background="#FF6E40"
android:paddingHorizontal="8dp"
app:layout_constraintLeft_toRightOf="@id/editc3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/morec3"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
...
</androidx.constraintlayout.widget.ConstraintLayout>
If you have a better way to do this design, I would really appreciate if you tell me which one.
The solution you propose is only possible using reflection, which should be avoided whenever possible. A better alternative is to have all the values together in a collection or array and do the operations based on their indices.
If you lose the indexes you can do it with a map
This way you simply have to call the function with the name of the input you want to augment. For example
onMore("c2")
.If you are looking for a more modern solution you could create a component using jetpack compose . Inside the component you can define a state and functions that modify it. This way of creating components will be familiar to you if you have used any declarative framework like react.js, flutter, etc.