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 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);
}
}

View File

@ -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");
}
}
}

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"?>
<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">
<data>
@ -8,7 +9,7 @@
type="io.n0x.android.java_mot_testing.FractionCalculator" />
</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_height="match_parent">
@ -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" />
<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_constraintTop_toBottomOf="@+id/button_div" />
<TextView
android:text="@{calc.denomResult}"
android:id="@+id/txt_denom_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_marginBottom="412dp"
android:text="@{calc.denomResult}"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.247"
app:layout_constraintStart_toEndOf="@+id/txt_equals"
app:layout_constraintStart_toStartOf="parent"
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
android:text="@{calc.enumRight}"
android:id="@+id/enumRight"
@ -104,7 +103,7 @@
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:text="@{calc.enumLeft}"
android:text="@{calc.enumRight}"
android:id="@+id/enumLeft"
android:layout_width="140dp"
android:layout_height="50dp"