下面两段标红加深的代码是重点:

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

Android之MVC——Model通知View去更新(实用)的更多相关文章

  1. MVC(Model(模型) View(视图) Controller(控制器))

    复习 1.      商品表 增删改查 index.php  add.php   view.php   edit.php   action.php 2.      MVC(Model(模型)  Vie ...

  2. MVC - Model - Controller - View

    一. Model 1.1 在ASP.NET MVC 中 model 负责的是所有与 "数据“  相关的的任务. 也可以把Model 看成是 ASP.NET  中三层模式的 BLL层 加 DA ...

  3. [Backbone]4. Model & View, toggle between Model and View. -- 1

    如上图所示: Server有Data都交给Models处理, 然后由Models给Views Data,让View去告诉DOM如何显示, 然后DOM显示HTML; View events update ...

  4. ASP.NET MVC轻教程 Step By Step 4——Model、View和Controller

    ASP.NET MVC中的Model(数据模型)主要包括定义数据结构.数据库读写.数据验证等等和对象处理相关的工作. 在解决方案资源管理器中找到Model文件夹,点击右键,添加一个新类,名为“Mess ...

  5. 使用WebFrom来模拟一些MVC的MODEL与View的数据交互功能

    MVC中有一点非常闪瞎人眼的功能就是,可以根据Model与View视图来直接将页面也数据模型进行绑定,那么我们在想客户端发送页面时不需要进行各种控件赋值,不需要操心怎么渲染,当客户提交表单给服务器时也 ...

  6. Spring MVC:Model、View、ModelAndView

    个人理解:View为服务器上的某个文件容器,可以为JSP,FTL等动态页面文件,甚至是媒体文件等等,单单是一个文件.Model的作用是存储动态页面属性,动态页面文件即View可以在Model中获取动态 ...

  7. What is the difference between Reactjs and Rxjs?--React is the V (View) in MVC (Model/View/Controller).

    This is really different, React is view library; and Rxjs is reactive programming library for javasc ...

  8. 2017年第1贴:EXT.JS使用MVC模式时,注意如何协调MODEL, STORE,VIEW,CONTROLLER的关系

    也调了快一天,死活找不到窍门. MODEL, STORE,VIEW的调置测试了很久,试了N种方法,不得其果. 最后,试着在APPLICATION里加入CONTROLLER, 在CONTROLLER里加 ...

  9. ASP.NET MVC之model传值view

    控制器中,我们有时会在知道用户名的情况下,再获取相关数据 例如: public ActionResult Index()        {            UserInfo Entity_Tem ...

随机推荐

  1. android拾遗——Android之Notification和NotificationManager

    1.使用系统自带的Notification //创建一个NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; Notific ...

  2. linux系统host修改

    有时候我们需要修改主机的host主机名,方便管理和识别自己的服务器,修改步骤如下: 第一步: vi  /etc/hosts 正常情况下,修改了第一步就可以了,如果通过hostname命令查看还是修改以 ...

  3. 2017冬季24集训模拟-2.A问题

    ————————————————————————————————————————题解 唯一没有想出来的题 我们发现以上两种操作 a0,a3,a6,a9……的相对位置不变 a1,a4,a7,a10……的 ...

  4. think组合查询AND和OR一起用

    如下示例: $_where 和 $where组合查询 $_where之间用OR $where之间用AND 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  5. JVM快速入门

    最近开始了全面的JAVA生态环境学习,因此,JVM的学习是必不可少的一个环节.和.NET的CLR一样,一起的JAVA应用均跑在JVM虚拟机上,不过相对我们只能干看看的CLR,JVM有很大的灵活性,可以 ...

  6. CodeForces 140C New Year Snowmen(堆)

    题面 CodeForces 题解 因为要保证两两不同,所以不能单纯的开堆来维护,堆维护一个二元组,个数为第一关键字,编号为第二关键字,对于一个相同的颜色,统计一下这个颜色的个数再用堆来维护就好了. # ...

  7. BZOJ 3172 [Tjoi2013]单词 AC自动机Fail树

    题目链接:[http://www.lydsy.com/JudgeOnline/problem.php?id=3172] 题意:给出一个文章的所有单词,然后找出每个单词在文章中出现的次数,单词用标点符号 ...

  8. 【洛谷】2324:[SCOI2005]骑士精神【IDA*】

    P2324 [SCOI2005]骑士精神 题目描述 输入输出格式 输入格式: 第一行有一个正整数T(T<=10),表示一共有N组数据.接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,* ...

  9. bzoj 3252: 攻略 -- 长链剖分+贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MB Description 题目简述:树版[k取方格数]   众所周知,桂木桂马是攻略之神,开启攻略之神 ...

  10. Elasticsearch中document的基础知识

    写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...