Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the data refresh process of MVVM architecture implemented by Android?

Shulou Source: shulou.com Published: 2022-06-02 21:43:10 10月01日 Update

This article mainly explains "Android to achieve MVVM architecture data refresh process is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Android to achieve MVVM architecture data refresh process is what" it!

Effect picture

Sample structure diagram

Code parsing imports dataBinding dataBinding {enabled = true} entity classes

Inherit BaseObservable

Public class Sensor extends BaseObservable

Add @ Bindable to the field

@ Bindable public String getTmpValue () {return tmpValue;}

Show picture

Picture add @ BindingAdapter

@ BindingAdapter ("tmpImage")

The example uses local pictures, not network pictures.

@ BindingAdapter ("tmpImage") public static void setTmpImage (ImageView view, int tmpImage) {view.setImageDrawable (view.getContext () .getResources () .getDrawable (tmpImage));}

Bind fields to the picture path

@ Bindable public int getTmpImage () {return tmpImage;}

Bind entity class

Set properties according to the contents of the @ BindingAdapter ("tmpImage") setting

All code of entity class

Public class Sensor extends BaseObservable {private String tmpValue; private String humValue; private String lightValue; private String humanValue; private String smokeValue; private String fireValue; private int tmpImage; private int humImage; private int lightImage; private int humanImage; private int smokeImage; private int fireImage; public Sensor () {} public Sensor (int tmpImage,int humImage,int lightImage,int humanImage,int smokeImage,int fireImage) {this.tmpImage = tmpImage; this.humImage = humImage This.lightImage = lightImage; this.humanImage = humanImage; this.smokeImage = smokeImage; this.fireImage = fireImage;} @ Bindable public String getTmpValue () {return tmpValue;} public void setTmpValue (String tmpValue) {this.tmpValue = tmpValue; notifyPropertyChanged (BR.tmpValue);} @ Bindable public String getHumValue () {return humValue } public void setHumValue (String humValue) {this.humValue = humValue; notifyPropertyChanged (BR.humValue);} @ Bindable public String getLightValue () {return lightValue;} public void setLightValue (String lightValue) {this.lightValue = lightValue; notifyPropertyChanged (BR.lightValue);} @ Bindable public String getHumanValue () {return humanValue } public void setHumanValue (String humanValue) {this.humanValue = humanValue; notifyPropertyChanged (BR.humanValue);} @ Bindable public String getSmokeValue () {return smokeValue;} public void setSmokeValue (String smokeValue) {this.smokeValue = smokeValue; notifyPropertyChanged (BR.smokeValue);} @ Bindable public String getFireValue () {return fireValue } public void setFireValue (String fireValue) {this.fireValue = fireValue; notifyPropertyChanged (BR.fireValue);} @ Bindable public int getTmpImage () {return tmpImage;} @ BindingAdapter ("tmpImage") public static void setTmpImage (ImageView view, int tmpImage) {view.setImageDrawable (view.getContext (). GetResources (). GetDrawable (tmpImage)) } @ Bindable public int getLightImage () {return lightImage;} @ BindingAdapter ("lightImage") public static void setLightImage (ImageView view,int lightImage) {view.setImageDrawable (view.getContext (). GetResources (). GetDrawable (lightImage));} @ Bindable public int getHumanImage () {return humanImage } @ BindingAdapter ("humanImage") public static void setHumanImage (ImageView view,int humanImage) {view.setImageDrawable (view.getContext (). GetResources (). GetDrawable (humanImage);} @ Bindable public int getSmokeImage () {return smokeImage;} @ BindingAdapter ("smokeImage") public static void setSmokeImage (ImageView view,int smokeImage) {view.setImageDrawable (view.getContext (). GetResources (). GetDrawable (smokeImage)) } @ Bindable public int getFireImage () {return fireImage;} @ BindingAdapter ("fireImage") public static void setFireImage (ImageView view,int fireImage) {view.setImageDrawable (view.getContext (). GetResources (). GetDrawable (fireImage));} @ Bindable public int getHumImage () {return humImage } @ BindingAdapter ("humImage") public static void setHumImage (ImageView view,int humImage) {view.setImageDrawable (view.getContext () .getResources () .getDrawable (humImage)) }} xml View VM

Receive data

Call the interface of the Handle class to receive sensor data (random data)

Private void InitData () {handle.setHandleDta (new Handle.HandleData () {@ Override public void getSensorValue (int [] value) {new Thread (() -) > {while (true) {try {Thread.sleep (5000) } catch (InterruptedException e) {e.printStackTrace ();} sensor.setTmpValue (value [0] + "℃"); sensor.setHumValue (value [1] + "RH"); sensor.setLightValue (value [2] + "LUX") Sensor.setSmokeValue (value [3] + "%"); sensor.setFireValue (value [4] + "%"); sensor.setHumanValue (value [5] = = 1? "someone": "nobody");}}) .start ();}});}

Send data

Establish an interface and call back data

Public interface HandleData {void getSensorValue (int [] value);} public void setHandleDta (HandleData handleDta) {int [] value = ReturnData (); handleDta.getSensorValue (value);}

Manufacturing data

Private void RefreshSensorValue () {thread = new Thread (()-> {while (true) {try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace () } / * temperature * / value [0] = RandomRange (35 Magazine 30); / * humidity * / value [1] = RandomRange (80 Magazine 75); / * Light value * / value [2] = RandomRange (120100) / * smoke * / value [3] = RandomRange (605.50); / * Flame * / value [4] = RandomRange (30.25); / * Infrared * / value [5] = RandomRange (2JEO); Log.d ("TAG", value [5] + ") }}); thread.start ();} bind view to data layer public class MainActivity extends AppCompatActivity {private ActivityMainBinding binding; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); binding = DataBindingUtil.setContentView (this,R.layout.activity_main); binding.setViewmodel (new ViewModel ()) }} Thank you for your reading. The above is the content of "what is the data refresh process for Android to implement MVVM architecture". After the study of this article, I believe you have a deeper understanding of what is the data refresh process for MVVM architecture implemented by Android. The specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Data pictures architecture processes content entities learning code fields interfaces examples views sensors lighting that is attributes ideas situations effects effect drawings Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft Xiaomi Docker OPPO Reno