Korrektes zuweisen der FractionCalculator zur DataBinding-Funktionalität

This commit is contained in:
_N0x 2021-06-29 18:03:00 +02:00
parent 73ea482776
commit eb6c30f3cd
4 changed files with 97 additions and 91 deletions

View File

@ -5,55 +5,82 @@ import android.util.Log;
import android.widget.TextView; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; 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 static final String TAG = FractionCalculator.class.getName();
private int enumLeft; private String denomLeft;
private int denomLeft; private String enumLeft;
private int enumRight; private String denomRight;
private int denomRight; private String enumRight;
public int enumResult; private String denomResult;
public int denomResult; private String enumResult;
public FractionCalculator(){
protected void doStuff() { Log.d(TAG, "new FractionCalculator created");
Log.d(TAG, "FractionCalculator started");
} }
public int getEnumLeft() { /// #### GETTER AND SETTER ####
return enumLeft; @Bindable
} public String getDenomLeft() {
public void setEnumLeft(int enumLeft) {
this.enumLeft = enumLeft;
}
public int getDenomLeft() {
return denomLeft; return denomLeft;
} }
public void setDenomLeft(int denomLeft) { public void setDenomLeft(String denomLeft) {
this.denomLeft = denomLeft; this.denomLeft = denomLeft;
notifyPropertyChanged(BR.denomLeft);
} }
public int getEnumRight() { @Bindable
return enumRight; public String getEnumLeft() {
return enumLeft;
} }
public void setEnumRight(int enumRight) { public void setEnumLeft(String enumLeft) {
this.enumRight = enumRight; this.enumLeft = enumLeft;
notifyPropertyChanged(BR.enumLeft);
} }
public int getDenomRight() { @Bindable
public String getDenomRight() {
return denomRight; return denomRight;
} }
public void setDenomRight(int denomRight) { public void setDenomRight(String denomRight) {
this.denomRight = 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);
}
} }

View File

@ -1,54 +1,52 @@
package io.n0x.android.java_mot_testing; package io.n0x.android.java_mot_testing;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; 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 { public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// For debugging
private static final String TAG = MainActivity.class.getName(); private static final String TAG = MainActivity.class.getName();
// Local calculator instance
private FractionCalculator calc;
FractionCalculator calc = new FractionCalculator();
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.ex9_data_binding); // create calculator instance
Log.d(TAG, "MainActivity started"); 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); findViewById(R.id.button_add).setOnClickListener(this);
calc.doStuff(); // Log create action
Log.d(TAG, "MainActivity started");
} }
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (v.getId() == R.id.button_add) { if (v.getId() == R.id.button_add) {
// works (without using bindings)
TextView textView = findViewById(R.id.txt_enum_result); TextView textView = findViewById(R.id.txt_enum_result);
textView.setText("10"); textView.setText("1234");
calc.enumResult = 5; // work with DataBinding
calc.denomResult = 6; calc.setDenomLeft("123123123");
//calc.setDenomLeft(10);
/*
enumLeft = 1;
denomLeft = 2;
enumRight = 3;
denomRight = 4;
//*/
} }
} }
} }

View File

@ -1,18 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools" <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<data> <data>
@ -8,7 +9,7 @@
type="io.n0x.android.java_mot_testing.FractionCalculator" /> type="io.n0x.android.java_mot_testing.FractionCalculator" />
</data> </data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
@ -53,43 +54,41 @@
android:id="@+id/txt_equals" android:id="@+id/txt_equals"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="48dp" android:layout_marginTop="60dp"
android:text="=" android:text="="
android:textSize="34sp" android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent" 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" />
<TextView
android:id="@+id/txt_enum_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="@{calc.enumResult}"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/txt_denom_result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_div" /> app:layout_constraintTop_toBottomOf="@+id/button_div" />
<TextView <TextView
android:text="@{calc.denomResult}"
android:id="@+id/txt_denom_result" android:id="@+id/txt_denom_result"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="3dp" android:layout_marginTop="3dp"
android:layout_marginBottom="412dp" android:layout_marginBottom="412dp"
android:text="@{calc.denomResult}"
android:textAlignment="center" android:textAlignment="center"
android:textSize="34sp" android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.247" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@+id/txt_equals"
app:layout_constraintTop_toBottomOf="@+id/txt_enum_result" /> app:layout_constraintTop_toBottomOf="@+id/txt_enum_result" />
<TextView
android:text="Test1234"
android:id="@+id/txt_enum_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/txt_denom_result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.247"
app:layout_constraintStart_toEndOf="@+id/txt_equals"
app:layout_constraintTop_toBottomOf="@+id/button_div" />
<EditText <EditText
android:text="@{calc.enumRight}" android:text="@{calc.enumRight}"
android:id="@+id/enumRight" android:id="@+id/enumRight"
@ -104,7 +103,7 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<EditText <EditText
android:text="@{calc.enumLeft}" android:text="@{calc.enumRight}"
android:id="@+id/enumLeft" android:id="@+id/enumLeft"
android:layout_width="140dp" android:layout_width="140dp"
android:layout_height="50dp" android:layout_height="50dp"