Android design pattern-MVC pattern-androidmvc

Source: Internet
Author: User

Android design pattern-MVC pattern-androidmvc

Speaking of the MVC pattern of Android design, it is estimated that many people are familiar with it. Here we will take a closer look at what MVC is going on and explain it using ListView as an example.

I,In-depth understandingMVC Concept

MVC is Model-View-Controller. M: Logical Model, V: view model, and C: controller.

In MVC mode, the class libraries of the system framework are divided into three types: Model, View, and Controller ). Model objects are responsible for establishing data structures and performing operations. The view object is responsible for rendering the corresponding graphic information on the screen and displaying it to the user. The Controller object intercepts user buttons, screen touch and other events, and coordinates Model objects and View objects.

The user interacts with the view. The view receives and feedbacks user actions. The view sends user requests to the corresponding controller, and the Controller determines which model to call, then, the model calls the corresponding business logic to process user requests. If data needs to be returned, the model returns the corresponding data to the Controller, and the Controller calls the corresponding view, in the end, the returned data is formatted and rendered by the view. The returned data can be displayed in a user experience.

A model can have multiple views, one view can have multiple controllers, and one controller can have multiple models.

(1) Model)

Model is the core part of an application system and represents all the functions to be implemented by the system. For example, in a video player, a model represents a video database and program function code for video playback. In a photo application, a model represents a photo database and the program function code for image viewing. In a telephone application, the Model represents a telephone number book and the program function code for dialing and sending text messages.

The Model is generated in xml file format under the values directory, or Java code can be generated directly by hard coding. View and Model are connected through a bridge Adapter.

(2) View)

View is a feedback sent by software applications to users. It represents graphics display, sound playback, and tactile feedback in software applications. The root node of the view is the application's own window. For example, a video player may contain the current picture, which is a view. Another view component may be the text title of the video. Another is some playback buttons, such as Stop, Start, and Pause.

View is generated in the xml file format in the layout directory and obtained using findViewById (). You can also generate a View directly using Java code by hard encoding.

(3) Controller)

The Controller responds to external events in software applications, including keyboard hitting, screen touch, and incoming calls. The Controller implements an event queue. Each external event is uniquely identified in the event queue. The framework removes and distributes events from the queue in sequence.

II. Implementation of ListView in typical MVC examples

In Android, the most typical MVC is ListView. The data to be displayed is Model, the ListView in the interface is View, and the control data is displayed as Controller in ListView.

(1) directly generate Java code through hard encoding. Here we will explain it directly in annotations.

Public class ArrayAdapterActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // The ListView in the interface is View. In this example, the ListView = new listView (this) is generated by using the Java code in a hard-coded manner ); // control how data is displayed in ListView as ControllerArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_expandable_list_item_1, getData (); // The View and Model communicate with each other through the Adap. To connect. ListView. setAdapter (adapter); setContentView (listView); // click the event. The Controller is responsible for listView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// position starts from 0 and obtains the content of the clicked item Toast. makeText (ArrayAdapterActivity. this, getData (). get (position), Toast. LENGTH_SHORT ). show () ;}}) ;}// the data Model to be displayed. The private List <String> getData () is generated by using the hard-coded Java code () {List <String> data = new ArrayList <String> (); data. add ("a"); data. add ("B"); data. add ("c"); data. add ("d"); return data ;}}

(2) how to retrieve resource files from View and Model

First, create the file activity_arrayadapter.xml In the res/layout folder. It can be seen that only one ListView is included, that is, the View.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/listview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:drawSelectorOnTop="false" /></LinearLayout>

Add a character array and Model to strings. xml in the res/values folder.

<?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="good">         <item>a</item>          <item>b</item>          <item>c</item>          <item>d</item>    </string-array></resources>

Activity Code. The MVC model is described in the annotations.

Public class ArrayAdapterActivity2 extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_arrayadapter); // The ListView in the interface is a View. The View is generated in the xml file format under the layout directory, and the ListView = (listView) findViewById (R. id. listview); // controls how data is displayed in ListView as ControllerArrayAdapter <String> adapter = new ArrayAdapter <Str Ing> (this, android. R. layout. simple_expandable_list_item_1, getData (); // The View and Model are connected through a bridge Adapter. ListView. setAdapter (adapter); // click the event. The Controller is responsible for listView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// position starts from 0 and obtains the content of the clicked item Toast. makeText (ArrayAdapterActivity2.this, getData (). get (position), Toast. LENGTH_SHORT ). show () ;}}) ;}// data Model to be displayed. The Model generates a private List <String> getData () in the values directory in the xml file format () {List <String> data = new ArrayList <String> (); Resources res = getResources (); // retrieves the String [] good = res. getStringArray (R. array. good); for (int I = 0; I <good. length; I ++) {data. add (good [I]);} return data ;}}

The MVC mode of Android must be understood in the project so that it can be thoroughly understood and used.

Here we have basically finished the MVC model of Android design patterns. If this article is helpful to you, please click "recommended" for support. If there are any errors, please leave a message and discuss them together.

Reprinted Please attach this article link: http://www.cnblogs.com/liqw/p/4175325.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.