This is an example of implementation MVC pattern in android development.
Calculator application
- MainActivity is the application execution, there is only CalculatorActivity, which is responsible for the GUI, event handling and display of visual objects.
- JModel: maintains state of the calculator.
- JView: display of model state, the state of the calculator.
- JController: receives user input via button onClick events, sends user input to Model, and sends "Model state" to myView.
Class JController:
Calculator application
- MainActivity is the application execution, there is only CalculatorActivity, which is responsible for the GUI, event handling and display of visual objects.
- JModel: maintains state of the calculator.
- JView: display of model state, the state of the calculator.
- JController: receives user input via button onClick events, sends user input to Model, and sends "Model state" to myView.
Class JController:
package com.example.mvcpatternandroid; import com.example.mvcpatternandroid.R; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; /** * copyright (c) jbohn */ public class JController implements OnClickListener { JModel myModel = null; JView myView = null; public JController(final JModel myModel, final JView myView) { this.myModel = myModel; this.myView = myView; this.myView.registerListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.button0: myModel.setModel('0'); break; case R.id.button1: myModel.setModel('1'); break; case R.id.button2: myModel.setModel('2'); break; case R.id.button3: myModel.setModel('3'); break; case R.id.button4: myModel.setModel('4'); break; case R.id.button5: myModel.setModel('5'); break; case R.id.button6: myModel.setModel('6'); break; case R.id.button7: myModel.setModel('7'); break; case R.id.button8: myModel.setModel('8'); break; case R.id.button9: myModel.setModel('9'); break; case R.id.buttonPlus: myModel.setModel('+'); break; case R.id.buttonDivide: myModel.setModel('/'); break; case R.id.buttonMinus: myModel.setModel('-'); break; case R.id.buttonTimes: myModel.setModel('*'); break; case R.id.buttonC: myModel.setModel('C'); break; case R.id.buttonEqual: myModel.setModel('='); break; } myView.setView(myModel.getModel()); } }
* Class JView
package com.example.mvcpatternandroid; import com.example.mvcpatternandroid.R; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; /** * @author QuocTrinh * copyright (c) jbohn */ public class JView extends LinearLayout { private EditText txtDisplay; LayoutInflater layoutInflater; public JView(Activity activity) { super(activity, null); this.setOrientation(VERTICAL); getInFlater(activity); getTxtView(); } private void getInFlater(Activity activity) { layoutInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } private void getTxtView() { layoutInflater.inflate(R.layout.display, this); txtDisplay = (EditText) findViewById(R.id.editString); } public void setView(String s) { txtDisplay.setText(s); } public void registerListener(OnClickListener onClickListener) { View view = (View) layoutInflater.inflate(R.layout.keyboard, this); final Button button0 = (Button) view.findViewById(R.id.button0); final Button button1 = (Button) view.findViewById(R.id.button1); final Button button2 = (Button) view.findViewById(R.id.button2); final Button button3 = (Button) view.findViewById(R.id.button3); final Button button4 = (Button) view.findViewById(R.id.button4); final Button button5 = (Button) view.findViewById(R.id.button5); final Button button6 = (Button) view.findViewById(R.id.button6); final Button button7 = (Button) view.findViewById(R.id.button7); final Button button8 = (Button) view.findViewById(R.id.button8); final Button button9 = (Button) view.findViewById(R.id.button9); final Button buttonC = (Button) view.findViewById(R.id.buttonC); final Button buttonPlus = (Button) view.findViewById(R.id.buttonPlus); final Button buttonEqual = (Button) view.findViewById(R.id.buttonEqual); final Button buttonMinus = (Button) view.findViewById(R.id.buttonMinus); final Button buttonTimes = (Button) view.findViewById(R.id.buttonTimes); final Button buttonDivide = (Button) view .findViewById(R.id.buttonDivide); button0.setOnClickListener(onClickListener); button1.setOnClickListener(onClickListener); button2.setOnClickListener(onClickListener); button3.setOnClickListener(onClickListener); button4.setOnClickListener(onClickListener); button5.setOnClickListener(onClickListener); button6.setOnClickListener(onClickListener); button7.setOnClickListener(onClickListener); button8.setOnClickListener(onClickListener); button9.setOnClickListener(onClickListener); buttonC.setOnClickListener(onClickListener); buttonPlus.setOnClickListener(onClickListener); buttonEqual.setOnClickListener(onClickListener); buttonMinus.setOnClickListener(onClickListener); buttonTimes.setOnClickListener(onClickListener); buttonDivide.setOnClickListener(onClickListener); } }
* Class JModel
package com.example.mvcpatternandroid; /** * @author QuocTrinh * copyright (c) jbohn */ public class JModel { private double accumulator, opr; private char op; public JModel() { accumulator = 0.0; } public void setModel(char c) { switch (c) { case 'C': opr = 0.0; accumulator = 0.0; break; case '+': ; case '-': ; case '*': ; case '/': op = c; opr = accumulator; accumulator = 0.0; break; case '=': switch (op) { case '+': accumulator = opr + accumulator; break; case '-': accumulator = opr - accumulator; break; case '*': accumulator = opr * accumulator; break; case '/': accumulator = opr / accumulator; break; default: } break; default: // Assume '0'..'9' digit accumulator = accumulator * 10.0 + (c - '0'); break; } } public String getModel() { return accumulator + ""; } }
Download Source code (http://jbohn.blogspot.com/)
any chance of looking at the complete source for this MVC example?
ReplyDeleteregards
this is download link: https://learn-java-together.googlecode.com/files/MVCPatternAndroid.7z
ReplyDeleteActually I posted source at the end of this entry, u can use a reader QR app to get it.
BR