From eb6c30f3cdf1c9b5e9dd851b8491a40b1434a190 Mon Sep 17 00:00:00 2001 From: _N0x Date: Tue, 29 Jun 2021 18:03:00 +0200 Subject: [PATCH] =?UTF-8?q?Korrektes=20zuweisen=20der=20FractionCalculator?= =?UTF-8?q?=20zur=20DataBinding-Funktionalit=C3=A4t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java_mot_testing/FractionCalculator.java | 81 ++++++++++++------- .../java_mot_testing/MainActivity.java | 46 +++++------ app/src/main/res/layout/activity_main.xml | 18 ----- app/src/main/res/layout/ex9_data_binding.xml | 43 +++++----- 4 files changed, 97 insertions(+), 91 deletions(-) delete mode 100644 app/src/main/res/layout/activity_main.xml diff --git a/app/src/main/java/io/n0x/android/java_mot_testing/FractionCalculator.java b/app/src/main/java/io/n0x/android/java_mot_testing/FractionCalculator.java index eb9d9ab..b0a904a 100644 --- a/app/src/main/java/io/n0x/android/java_mot_testing/FractionCalculator.java +++ b/app/src/main/java/io/n0x/android/java_mot_testing/FractionCalculator.java @@ -5,55 +5,82 @@ import android.util.Log; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; +import androidx.databinding.BaseObservable; +import androidx.databinding.Bindable; -public class FractionCalculator{ +public class FractionCalculator extends BaseObservable{ private static final String TAG = FractionCalculator.class.getName(); - private int enumLeft; - private int denomLeft; - private int enumRight; - private int denomRight; - public int enumResult; - public int denomResult; + private String denomLeft; + private String enumLeft; + private String denomRight; + private String enumRight; + private String denomResult; + private String enumResult; - - protected void doStuff() { - Log.d(TAG, "FractionCalculator started"); + public FractionCalculator(){ + Log.d(TAG, "new FractionCalculator created"); } - public int getEnumLeft() { - return enumLeft; - } - - public void setEnumLeft(int enumLeft) { - this.enumLeft = enumLeft; - } - - public int getDenomLeft() { + /// #### GETTER AND SETTER #### + @Bindable + public String getDenomLeft() { return denomLeft; } - public void setDenomLeft(int denomLeft) { + public void setDenomLeft(String denomLeft) { this.denomLeft = denomLeft; + notifyPropertyChanged(BR.denomLeft); } - public int getEnumRight() { - return enumRight; + @Bindable + public String getEnumLeft() { + return enumLeft; } - public void setEnumRight(int enumRight) { - this.enumRight = enumRight; + public void setEnumLeft(String enumLeft) { + this.enumLeft = enumLeft; + notifyPropertyChanged(BR.enumLeft); } - public int getDenomRight() { + @Bindable + public String getDenomRight() { return denomRight; } - public void setDenomRight(int denomRight) { + public void setDenomRight(String denomRight) { this.denomRight = denomRight; + notifyPropertyChanged(BR.denomRight); } - // GETTER AND SETTER + @Bindable + public String getEnumRight() { + return enumRight; + } + public void setEnumRight(String enumRight) { + this.enumRight = enumRight; + notifyPropertyChanged(BR.enumRight); + } + + @Bindable + public String getDenomResult() { + return denomResult; + } + + public void setDenomResult(String denomResult) { + this.denomResult = denomResult; + notifyPropertyChanged(BR.denomResult); + } + + @Bindable + public String getEnumResult() { + return enumResult; + } + + public void setEnumResult(String enumResult) { + this.enumResult = enumResult; + notifyPropertyChanged(BR.enumResult); + } } diff --git a/app/src/main/java/io/n0x/android/java_mot_testing/MainActivity.java b/app/src/main/java/io/n0x/android/java_mot_testing/MainActivity.java index c679ed0..865efe6 100644 --- a/app/src/main/java/io/n0x/android/java_mot_testing/MainActivity.java +++ b/app/src/main/java/io/n0x/android/java_mot_testing/MainActivity.java @@ -1,54 +1,52 @@ package io.n0x.android.java_mot_testing; -import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; -import androidx.databinding.BindingAdapter; +import androidx.databinding.DataBindingUtil; + +import io.n0x.android.java_mot_testing.databinding.Ex9DataBindingBinding; public class MainActivity extends AppCompatActivity implements View.OnClickListener { - + // For debugging private static final String TAG = MainActivity.class.getName(); - - - - FractionCalculator calc = new FractionCalculator(); + // Local calculator instance + private FractionCalculator calc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.ex9_data_binding); - Log.d(TAG, "MainActivity started"); + // create calculator instance + calc = new FractionCalculator(); + // set a value for testing + calc.setEnumResult("0"); + calc.setDenomResult("0"); - // Button Listeners + // Bind instance of FractionCalculator to view + Ex9DataBindingBinding binding = DataBindingUtil.setContentView(this, R.layout.ex9_data_binding); + binding.setCalc(calc); + + // Button Listeners findViewById(R.id.button_add).setOnClickListener(this); - calc.doStuff(); + // Log create action + Log.d(TAG, "MainActivity started"); } @Override public void onClick(View v) { if (v.getId() == R.id.button_add) { + // works (without using bindings) TextView textView = findViewById(R.id.txt_enum_result); - textView.setText("10"); + textView.setText("1234"); - calc.enumResult = 5; - calc.denomResult = 6; - - //calc.setDenomLeft(10); -/* - enumLeft = 1; - denomLeft = 2; - enumRight = 3; - denomRight = 4; - - - //*/ + // work with DataBinding + calc.setDenomLeft("123123123"); } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml deleted file mode 100644 index 4fc2444..0000000 --- a/app/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/ex9_data_binding.xml b/app/src/main/res/layout/ex9_data_binding.xml index ffbcd75..6f2a305 100644 --- a/app/src/main/res/layout/ex9_data_binding.xml +++ b/app/src/main/res/layout/ex9_data_binding.xml @@ -1,5 +1,6 @@ - @@ -8,7 +9,7 @@ type="io.n0x.android.java_mot_testing.FractionCalculator" /> - @@ -53,43 +54,41 @@ android:id="@+id/txt_equals" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginTop="48dp" + android:layout_marginTop="60dp" android:text="=" android:textSize="34sp" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintHorizontal_bias="0.183" + app:layout_constraintHorizontal_bias="0.181" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/button_div" /> + + - -