The MVC pattern of Android design mode

Source: Internet
Author: User

When it comes to the MVC pattern of Android design patterns, it's estimated that a lot of people are more familiar with it, and here's an in-depth look at what MVC is all about, with the ListView as an example.

I. In- depth understanding of the MVC concept

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

In MVC mode, the class library of the system framework is divided into 3 types: model, view, controller. The model object is responsible for establishing the data structure and corresponding behavior manipulation processing. The View object is responsible for rendering the corresponding graphical information on the screen to show the user. The Controller object is responsible for intercepting the user's keystrokes and screen touches, coordinating the model object and the View object.

The user interacts with the view, the view receives and feeds back the user's action, the view passes the user's request to the appropriate controller, the controller decides which model to call, and the model calls the corresponding business logic to process the user request, and if it needs to return the data, the model will return the corresponding data to the controller. The corresponding view is called by the Controller, and the returned data is eventually formatted and rendered by the view, and the returned data can be fully displayed to the user with the added user experience effect.

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

(1), model

Model is a core part of an application system and represents all the functions that the system actually implements. For example: In a video player, the model represents a video database and the program function code that plays the video; In the photo application, the model represents a photo database, and the program function code when looking at the picture. In a telephony app, model represents a phone book, and program function code that calls and sends text messages.

The model is generated in the values directory through the XML file format, or it can be hardcoded directly into Java code. The view and model are connected by bridge adapter.

(2), Views (view)

View is a feedback result that the software application transmits to the user. It represents the functions of graphical display, sound playback, tactile feedback, etc. in software applications. The root node of the view is the application's own window. For example, the video player may contain the currently playing screen, which is a view. Another view component might be the text caption for the video. Another is a few play buttons, such as: Stop, Start, pause and other buttons.

The view is generated in the layout directory through an XML file format, obtained with Findviewbyid (), or directly from Java code in hard-coded ways.

(3), controllers (Controller)

Controller in the software application is responsible for the response to external events, including: keyboard percussion, screen touch, call incoming and so on. The controller implements an event queue, and each external event is uniquely identified in the event queue. The framework moves the events out of the queue and distributes them sequentially.

Second, the implementation of the typical MVC example of the ListView

The most typical MVC in Android is the ListView, the data to be displayed is model, the ListView in the interface is the view, and the control data is displayed in the ListView as the controller.

(1), by hard-coded way direct Java code generation method, here directly in the comments explained

public class Arrayadapteractivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//The ListView in the interface is the view, which is generated by hard-coded direct Java code to generate a ListView ListView = New ListView (This) ///control how the data is displayed in the ListView is controllerarrayadapter<string> adapter = new Arrayadapter<string> (this, Android. R.layout.simple_expandable_list_item_1, GetData ());//view and model are connected by bridge adapter. Listview.setadapter (adapter); Setcontentview (ListView);//Click event, Controller is responsible for Listview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, view view, int position, long ID) {/ /position is starting from 0, gets the content of the click Item Toast.maketext (Arrayadapteractivity.this, GetData (). get (position), Toast.length_short ). Show ();}}); To display the data model, a hard-coded direct Java code generates the private list<string> getData () {list<string> data = new arraylist< String> ();d ata.add ("a");d Ata.add ("B");d Ata.add ("C");d Ata.add ("D"); return data;}

(2), view and model models take resource file mode

To create a file Activity_arrayadapter.xml in the Res/layout folder, you can see that only one ListView is included, that is, the views 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:o rientation= "vertical" >    <listview        android:id= "@+id/listview" android:layout_width= "Fill_        Parent "        android:layout_height=" fill_parent "        android:drawselectorontop=" false "/></linearlayout >

Add a character array to strings.xml under the Res/values folder, and model

<?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, explaining the MVC model usage in comments

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, View is generated in the layout directory through the XML file format, with Findviewbyid () to get the ListView ListView = (ListView) Findviewbyid (R.id.listview);// Control how the data is displayed in the ListView is controllerarrayadapter<string> adapter = new Arrayadapter<string> (this, Android. R.layout.simple_expandable_list_item_1, GetData ());//view and model are connected by bridge adapter. Listview.setadapter (adapter);//Click event, Controller is responsible for Listview.setonitemclicklistener (new Onitemclicklistener () {@ overridepublic void Onitemclick (adapterview<?> parent, view view, int position, long ID) {//position is starting from 0, get click Ite Contents of M Toast.maketext (Arrayadapteractivity2.this, GetData (). get (position), Toast.length_short). Show ();});} The data to be displayed Model,model generated in the values directory by the XML file format private list<string> getData () {list<string> data = new ArrayList <String> (); Resources Res =getResources ();//Take an XML file format character array string[] Good=res.getstringarray (R.array.good); for (int i=0;i<good.length;i++) { Data.add (Good[i]);} return data;}}

The Android MVC model needs to be understood in the project so that it can be understood thoroughly and ingenious.

To the basic introduction of the Android design model of the MVC model, if this article is helpful to you please click "Recommended" support, if there are any errors please comment, discuss together.

Reprint please attach this article link: http://www.cnblogs.com/liqw/p/4175325.html

The MVC pattern of Android design mode

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.