} dependencies { classpath 'com.android.tools.build:gradle:1.5.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
type="ca.iprojet.myairbuddyandi.CylinderType"/> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> Start your new layout here… </LinearLayout> </layout> POJO / Data Object POJO alias’ through this layout Case sensitive through this layout Within same single layout
android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:text="@={cylinderType.cylinderType}" android:id="@+id/editTextCT" CylinderType.getCylinderType() CylinderType.setCylinderType() 1 way: “@{}“ 2 way: “@ ={}“ Not required for Data Binding But very useful to avoid findViewById() Use Text Input Layout And error message Will generate class “CylinderTypeActivityBinding “
ViewHolder extends RecyclerView.ViewHolder { private SacRmvBinding binding; private ViewHolder(View itemView) { super(itemView); binding = DataBindingUtil.bind(itemView); RadioButton radioButton = binding.RadioButton; radioButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mLastCheckedRb != null) { mLastCheckedRb.setChecked(false); } mLastCheckedRb = (RadioButton) view; int position = getAdapterPosition(); mSacRmv = mSacRmvList.get(position); } }); } Set the previous Radio Button @ ON to OFF The new Radio Button becomes the previous Radio Button @ ON POJO to be updated in the Database with the Radio Button @ ON
& Off) <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:orientation="horizontal" app:setOnCheckedChangeListener="@{dive.getOnRadioGroupChanged}"> <TextView android:id="@+id/salinityH" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/blue" android:textSize="18sp" android:text="@string/hint_salinity"/> 1 Way Data Binding Used to set mHasMyDataChanged Other solution would be to keep value before and after
& Off) Cont’d <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="0.5" android:id="@+id/salinityS" android:text="@string/radio_button_salt" android:checked="@={dive.salinity}"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="0.5" android:id="@+id/salinityF" android:text="@string/radio_button_fresh" android:checked="@{dive.fresh}"/> </RadioGroup> 2 Way Data Binding Salinity is read from the POJO and set to the POJO 1 Way Data Binding
& Off) Cont’d public class MyRadioGroupWatcher implements RadioGroup.OnCheckedChangeListener { public static final String LOG_TAG = "MyRadioGroupWatcher"; @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub } }
& Off) Cont’d • 2 Way Data Binding public boolean getSalinity() {return mSalinity;} public void setSalinity(boolean salinity) {mSalinity = salinity;} • 1 Way Data Binding (returning the inverted value of Salinity) public boolean getFresh() {return !mSalinity;} 4 • Note: No setFresh() needed because it is using 1 Way Data Binding!
app:ms_floatingLabelColor="@color/blue" app:ms_multiline="true" app:ms_enableFloatingLabel="true" app:ms_enableErrorLabel="false" app:ms_floatingLabelText="@string/hint_status" app:ms_hint="@string/hint_status" app:ms_baseColor="@color/light_blue" app:setOnItemSelectedListener="@{dive.getOnSpinnerChangedStatus}" app:adapter="@{dive.AdapterStatus}" /> 2 Way Data Binding To load the Spinner Adapter 1 Way Data Binding To set the listener 1 Way Data Binding
mStatusPosition;} public void setStatusPosition(int statusPosition) {mStatusPosition = statusPosition;} Must turn the value into a position Must turn the position into a value
AdapterView.OnItemSelectedListener { public static final String LOG_TAG = "MySpinnerWatcher"; @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
model • Otherwise the listener kicks in • And your POJO thinks your data has been modified • Do not use findViewById • Do not use mBinding.editTextXX if (mDive.getDiveNo() == MyConstants.zeroL) { // Set the default values mDive.setLogBookNo(mAirDa.getLastLogBookNo() + 1); mDive.setDate(MyFunctions.getTodaysDate()); mDive.setStatus(getString(R.string.default_status)); // Plan
with Hint and Error Message if (!mBinding.editTextE.getText().toString().trim().isEmpty() && !isValidEmail(mDiver.getEmail())) { mBinding.editTextE.setError(getString(R.string.msg_email)); requestFocus(mBinding.editTextE); return false; } else { return true; }
already in the POJO / Data Object if (!isValidLogBookNo(mDive.getLogBookNo())) { mBinding.editTextLBN.setError(getString(R.string.msg_log_book_no)); requestFocus(mBinding.editTextLBN); return false; } else { return true; }
• In order to display warning message “Are you sure you want to cancel?” • Must used Reusable Listener • TextWatcher • SpinnerWatcher • RadioGroupWatcher • Others to develop!
Could not find a way to use the mBinding.editTextXX and Text Input Layout • Workaround is to work with the ArrayList<POJO> since the data is already in the POJOs • And use Toast to display error message
import ca.iprojet.myairbuddyandi.databinding. CylinderActivityBinding; • Same with private DiveActivityBinding mBinding = null; Generated Bindable Library containing all aliases on the layouts