This commit is contained in:
_N0x 2021-06-29 21:49:56 +02:00
parent c62b6123d5
commit e70b98a26b
3 changed files with 37 additions and 38 deletions

View File

@ -1,29 +1,23 @@
package io.n0x.android.java_mot_testing; package io.n0x.android.java_mot_testing;
import android.annotation.SuppressLint;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil; import androidx.databinding.DataBindingUtil;
import io.n0x.android.java_mot_testing.databinding.Ex9DataBindingBinding; import io.n0x.android.java_mot_testing.databinding.Ex9DataBindingBinding;
import io.n0x.android.java_mot_testing.helper.Converter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { public class MainActivity extends AppCompatActivity{
// For debugging // For debugging
private static final String TAG = MainActivity.class.getName(); private static final String TAG = MainActivity.class.getName();
// Local calculator instance
private FractionCalculator calc;
private int test;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// create calculator instance // Local calculator instance
calc = new FractionCalculator(); FractionCalculator calc = new FractionCalculator();
// set a value for testing // set a value for testing
calc.setEnumResult(0); calc.setEnumResult(0);
@ -32,37 +26,16 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// Bind instance of FractionCalculator to view // Bind instance of FractionCalculator to view
Ex9DataBindingBinding binding = DataBindingUtil.setContentView(this, R.layout.ex9_data_binding); Ex9DataBindingBinding binding = DataBindingUtil.setContentView(this, R.layout.ex9_data_binding);
binding.setCalc(calc); binding.setCalc(calc);
// set initial value for the arithmetical method
calc.setArithMethod(FractionCalculator.ARTIH.ADD);
// Button Listeners /*
findViewById(R.id.button_add).setOnClickListener(this); // Converter to get null values
findViewById(R.id.button_sub).setOnClickListener(this); Converter conv = new Converter();
findViewById(R.id.button_mult).setOnClickListener(this); binding.setConv(conv);
findViewById(R.id.button_div).setOnClickListener(this); //*/
findViewById(R.id.button_calc).setOnClickListener(this);
// Log create action // Log create action
Log.d(TAG, "MainActivity started"); Log.d(TAG, "MainActivity started");
} }
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button_add:
calc.setArithMethod(FractionCalculator.ARTIH.ADD);
break;
case R.id.button_sub:
calc.setArithMethod(FractionCalculator.ARTIH.SUBTRACT);
break;
case R.id.button_mult:
calc.setArithMethod(FractionCalculator.ARTIH.MULTIPLICATE);
break;
case R.id.button_div:
calc.setArithMethod(FractionCalculator.ARTIH.DIVIDE);
break;
case R.id.button_calc:
calc.calculateResult();
break;
}
}
} }

View File

@ -0,0 +1,18 @@
package io.n0x.android.java_mot_testing.helper;
import androidx.databinding.InverseMethod;
public class Converter {
@InverseMethod("toInt")
public static String toString(int val){
if(val == 0){
return null;
}
return String.valueOf(val);
}
public static int toInt(String val){
if(val == null || val.isEmpty())
return 0;
return Integer.parseInt(val);
}
}

View File

@ -7,6 +7,9 @@
<variable <variable
name="calc" name="calc"
type="io.n0x.android.java_mot_testing.FractionCalculator" /> type="io.n0x.android.java_mot_testing.FractionCalculator" />
<variable
name="conv"
type="io.n0x.android.java_mot_testing.helper.Converter" />
</data> </data>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
@ -14,6 +17,7 @@
android:layout_height="match_parent"> android:layout_height="match_parent">
<Button <Button
android:onClick="@{() -> calc.setArithMethod(io.n0x.android.java_mot_testing.FractionCalculator.ARTIH.ADD)}"
android:id="@+id/button_add" android:id="@+id/button_add"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -26,6 +30,7 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<Button <Button
android:onClick="@{() -> calc.setArithMethod(io.n0x.android.java_mot_testing.FractionCalculator.ARTIH.SUBTRACT)}"
android:id="@+id/button_sub" android:id="@+id/button_sub"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -36,6 +41,7 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<Button <Button
android:onClick="@{() -> calc.setArithMethod(io.n0x.android.java_mot_testing.FractionCalculator.ARTIH.MULTIPLICATE)}"
android:id="@+id/button_mult" android:id="@+id/button_mult"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -46,6 +52,7 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<Button <Button
android:onClick="@{() -> calc.setArithMethod(io.n0x.android.java_mot_testing.FractionCalculator.ARTIH.DIVIDE)}"
android:id="@+id/button_div" android:id="@+id/button_div"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -136,6 +143,7 @@
app:layout_constraintTop_toBottomOf="@+id/enumRight" /> app:layout_constraintTop_toBottomOf="@+id/enumRight" />
<Button <Button
android:onClick="@{() -> calc.calculateResult()}"
android:id="@+id/button_calc" android:id="@+id/button_calc"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"