Android之MVC——Model通知View去更新(实用)
2013-07-09 11:59
218 查看
下面两段标红加深的代码是重点:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import java.util.Observable; import java.util.Observer; // you must implements Observer public class ObserverExample extends Activity implements Observer { // Button variables for our 4 UI buttons which perform touchdowns and field // goals for the Home and Away teams. Button button1; Button button2; Button button3; Button button4; // Score variable for the Score object we'll be using to track and report // out both teams' scores. Score score; // TextView variables for our scoreboard display, #2 is Home score, #4 is // Vistors score. TextView textView2; TextView textView4; // Set some constants to make the code more human-readable. private static final int HOME = 1; private static final int VISITOR = 2; @Override public void update(Observable observable, Object data) { // Set the score to the current score each time there's a change. // Creating a helper method is a nice way to simplify the code here. setScore(); } // Grabs the score for the Home team and Away team and displays them in // the correct TextView's of the scoreboard. private void setScore() { textView2.setText(String.valueOf(score.getScore(HOME))); textView4.setText(String.valueOf(score.getScore(VISITOR))); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create a new score object, this will contain all of the logic to // track and report out both team's scores. score = new Score(); // Score is an Observable class, therefore, we can use the method // addObserver to add this Activity as an Observer using it's local // context. This is the magic sauce that enables instant updates // whenever the Score changes. When the Score reports a change, the // update() method runs in this Activity. score.addObserver(this); // This code ties in our TextView variables to the actual ID's of the UI // elements themselves. textView2 = (TextView) findViewById(R.id.textView2); textView4 = (TextView) findViewById(R.id.textView4); // Ties in the ID of the button to our variable set above. button1 = (Button) findViewById(R.id.button1); // Creates a new instance of OnClickListener which fires the onClick() // method upon, well, a click. button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Update the score by running the touchDown() method for the // Home team. score.touchDown(HOME); } }); // Same idea as above repeated for buttons 2 - 4. button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Update the score by running the fieldGoal() method for the // Home team. score.fieldGoal(HOME); } }); button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { score.touchDown(VISITOR); } }); button4 = (Button) findViewById(R.id.button4); button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { score.fieldGoal(VISITOR); } }); } }
import java.util.Observable; // In order to report changes to any interested objects, such as Activities, we // need to extend the Observable class. This enables other objects to register // themselves as an Observer by using the addObserver() method. public class Score extends Observable { // Initialize the score, scores always start at zero. private int home_score = 0; private int visitor_score = 0; public Score() { } // Grant 1 point to the team based on the variable fed in HOME == 1 // and VISITOR == 2; public void fieldGoal(int team) { switch (team) { case 1: this.home_score = this.home_score + 1; break; case 2: this.visitor_score = this.visitor_score + 1; break; } triggerObservers(); } // Returns the score of the Home or Visitor teams depending on which // variable is fed. public int getScore(int team) { switch (team) { case 1: return home_score; case 2: return visitor_score; } return 0; } // Adds six points to score of the team fed in as a variable, HOME == 1 and // VISITOR == 2 public void touchDown(int team) { switch (team) { case 1: this.home_score = this.home_score + 6; break; case 2: this.visitor_score = this.visitor_score + 6; break; } triggerObservers(); } // Create a method to update the Observerable's flag to true for changes and // notify the observers to check for a change. These are also a part of the // secret sauce that makes Observers and Observables communicate // predictably. private void triggerObservers() { setChanged(); notifyObservers(); } }
相关文章推荐
- MVP 模式(Model-View-Presenter)可以说是 MVC 模式(Model-View-Controller)在 Android 开发上的一种变种、进化模式。
- 【框架篇】mvc、mvp、mvvm使用关系总结 原创 2016年03月27日 10:04:21 标签:android /架构 9837 MVC MVC全名是Model View Controller,
- ASP.NET MVC(模型(Model),视图(View)和控制Controller)实践:实现身份验证权限管理
- 【移动开发】Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewFlow)
- Android 如何实现带滚动条的TextView,在更新文字时自动滚动到最后一行?
- Android新线程中更新主线程UI中的View方法汇总
- Android的Notification通知的简单实用
- Android显示系统之View与SurfaceView更新屏幕的区别
- 安卓自动更新,静默更新,替换友盟更新可以在通知栏里显示更新下载进度,几行代码快速实现Android下载更新
- EF中更新操作 ID自增但不是主键 ;根据ViewModel更新实体的部分属性
- Android架构组件三 Android Architecture Components ViewModel组件解析
- Android Databinding:再见Presenter,你好ViewModel
- QAbstractTableModel ------ Model-View-Control(MVC)架构(3)
- Android 如何实现带滚动条的TextView,在更新文字时自动滚动到最后一行
- Android开发RecycleView的适配器(adapter)无法更新集合中数据
- [转]APS.netMVC的ViewModel问题
- 在ASP.NET MVC中使用Knockout实践02,组合View Model成员、Select绑定、通过构造器创建View Model,扩展View Model方法
- Android---RecyclerView简单实用(上)
- MVC教程:MVC---View处理页面;Control处理业务逻辑;Model处理数据
- 【转】【英文】ASP.NET MVC View Model Patterns