Android creates a detail page, as well as the argument app

Source: Internet
Author: User

Hello everyone.. I've been wondering if there are any design patterns that can be used to create a detail page, until recently I looked at some of the blog posts and books related to design patterns, and I finally found the following apps.

Below we introduce a ListView click to appear a detail page function, the function is simple, but we look at the thought.

First we implement the custom listview,listitem inside there is a textview, a edittext, a checkbox, corresponding to the data is a crime class. There's a lot of ListItem in the ListView, And the corresponding is the Crimelist class, in order to facilitate the creation of a large number of crime objects, we used the Factory mode to achieve this crimelist, and this factory is a program has and only one, for the implementation of the factory class we used the singleton mode.

Factory-implemented code

 Public classCrimelab {PrivateArraylist<crime>Mcrimes; Private StaticCrimelab Scrimelab; PrivateContext Mappcontext; PrivateCrimelab (Context appContext) {Mappcontext=AppContext; Mcrimes=NewArraylist<crime>();  for(inti=0;i<100;i++) {Crime C=NewCrime (); C.settitle ("Crime #" +i); C.setsolved (i%2==0);        Mcrimes.add (c); }    }     PublicArraylist<crime>Getmcrimes () {returnMcrimes; }     PublicCrime getcrime (UUID id) { for(Crime c:mcrimes) {if(C.getid (). Equals (ID))returnC; }        return NULL; }     Public StaticCrimelab Get (Context c) {if(scrimelab==NULL) {Scrimelab=NewCrimelab (C.getapplicationcontext ()); }        returnScrimelab; }}

So how do we transfer the data from this crime class to the ListView, where we need a adapter to connect the ListView and our data mcrimes, and according to our needs, we can implement Arrayadapter Subclass and make a copy of the GetView method, the GetView method is called when the ListView has a new ListItem in the interface we see, when our crimeadapter is dedicated to Crimelistfragment, We can let this crimeadapter as a member of the Crimelistfragment class to write, then we in crimeadapter only need to transmit the necessary data mcrimes to it as a parameter.

Private classCrimeadapterextendsArrayadapter<crime>    {         PublicCrimeadapter (arraylist<crime>crimes) {            Super(Getactivity (), 0, crimes); } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {            if(convertview==NULL) {Convertview= Getactivity (). Getlayoutinflater (). Inflate (R.layout.crime_item,NULL); } Crime C=GetItem (position); TextView Titleview=(TextView) Convertview.findviewbyid (r.id.crime_title);            Titleview.settext (C.gettitle ()); TextView Dateview=(TextView) Convertview.findviewbyid (r.id.crime_date);            Dateview.settext (C.getmdate (). toString ()); CheckBox Solvebox=(CheckBox) Convertview.findviewbyid (r.id.isresolved);            Solvebox.setchecked (c.getsolved ()); returnConvertview; }    }

At the same time we want to implement this Adatper Onlistitemclick method, let activity jump, but activity jump time to pass through Putextra in intent inside the data. It is understandable that data is passed through intent between activity and activity, but we use a structure like fragment in the activity to fragment where we get the data from extra. You would have thought of using getactivity, yes. In fragment, it is possible to use getactivity for activity data, but this creates a strong coupling, and our fragment Can only be used to have the corresponding extra intent activity, in order to solve the coupling, the better way is that we put the data in their bundle, each fragment has a default bundle is argument, has the appropriate method to set up and get. First look at the code inside the crimefragment.

 Public Static crimefragment newinstance (UUID crimeid)    {        new  Bundle ();        Args.putserializable (Extra_crime_id,crimeid);         New crimefragment ();        Fragment.setarguments (args);         return fragment;    }

We typically use the Newinstance method name if we want to return an object and want to do something special with the object. With this method in Crimefragment, we can use this method every time we only need to pass the data between the activity with intent, and we want to add a new instance of fragment to the activity. Note that each time we transmit information, whether it is activity to activity, or to fragment, we only pass the UUID, which reminds me of the MVC pattern, the module is controlled by crimelist The controller is the individual acivity,fragment,intent;view is the control and layout. As long as our controller has a unique logo, where we can communicate with the module. Don't know this mode is not the best and have better please tell me

Android creates a detail page, as well as an app for argument

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.