Explanation of panoramio Implementation of the Ghost Map photo application in the android open-source code (4)

Source: Internet
Author: User

This article continues to explain the implementation of panoramio, mainly introducing imageadapter. java and imagelist. the two java files implement the following interface. The figure on the left shows the effect after the data is loaded from the network:


Imageadapter inherits from the baseadapter class to implement the image adapter function, while imagelist inherits from the listactivity class to display the searched image information in the form of a list. Before the introduction, we will first popularize the knowledge of datasetobserver and datasetobservable.

As you can vaguely guess from the name, datasetobserver implements the observer role (observer) in the Observer mode ). When a dataset changes or becomes invalid, the method in datasetobserver is called back. A typical dataset includes cursor and adapter. when an object is to be added to datasetobservable, this object must be inherited from datasetobserver, datasetobserver is an abstract class defined as follows:

Public abstract class datasetobserver {<br/>/** <br/> * this method is called when the entire data set has changed, <br/> * most likely through a call to {@ link cursor # requery ()} On A {@ link cursor }. <br/> */<br/> Public void onchanged () {<br/> // do nothing <br/>}</P> <p>/** <br/> * this method is called when the entire data becomes invalid, <br/> * most likely through a call to {@ link cursor # deactivate ()} or {@ link cursor # Close ()} on a <br/> * {@ link cursor }. <br/> */<br/> Public void oninvalidated () {<br/> // do nothing <br/>}< br/>}

Datasetobservable implements the object role (subject) in the Observer mode. It is a specific implementation of the observable and provides methods to call various callback functions in datasetobserver. The definition is as follows:

Public class datasetobservable extends observable <datasetobserver >{< br/>/** <br/> * invokes onchanged on each observer. called when the data set being observed has <br/> * changed, and which when read contains the new State of the data. <br/> */<br/> Public void policychanged () {<br/> synchronized (mobservers) {<br/> for (datasetobserver observer: mobservers) {<br/> observer. onchanged (); <br/>}</P> <p>/** <br/> * invokes oninvalidated on each observer. called when the data set being monitored <br/> * has changed such that it is no longer valid. <br/> */<br/> Public void policyinvalidated () {<br/> synchronized (mobservers) {<br/> for (datasetobserver observer: mobservers) {<br/> observer. oninvalidated (); <br/>}< br/>}

Note that the observable is defined in the Android. database package, instead of the observable in JDK. Its definition is as follows:

Package android. database; </P> <p> Import Java. util. arraylist; </P> <p>/** <br/> * provides methods for (un) Registering arbitrary observers in an arraylist. <br/> */<br/> public abstract class observable <t >{< br/>/** <br/> * the list of observers. an observer can be in the list at most <br/> * once and will never be null. <br/> */<br/> protected final arraylist <t> mobservers = new arraylist <t> (); </P> <p>/** <br/> * adds an observer to the list. the observer cannot be null and it must not already <br/> * be registered. <br/> * @ Param observer the observer to register <br/> * @ throws illegalargumentexception the observer is null <br/> * @ throws illegalstateexception the observer is already registered <br/> */<br/> Public void registerobserver (T observer) {<br/> If (Observer = NULL) {<br/> throw new illegalargumentexception ("the observer is null. "); <br/>}< br/> synchronized (mobservers) {<br/> If (mobservers. contains (observer) {<br/> throw new illegalstateexception ("Observer" + observer + "is already registered. "); <br/>}< br/> mobservers. add (observer); <br/>}</P> <p>/** <br/> * removes a previusly registered observer. the observer must not be null and it <br/> * must already have been registered. <br/> * @ Param observer the observer to unregister <br/> * @ throws illegalargumentexception the observer is null <br/> * @ throws illegalstateexception the observer is not yet registered <br /> */<br/> Public void unregisterobserver (T observer) {<br/> If (Observer = NULL) {<br/> throw new illegalargumentexception ("the observer is null. "); <br/>}< br/> synchronized (mobservers) {<br/> int Index = mobservers. indexof (observer); <br/> If (Index =-1) {<br/> throw new illegalstateexception ("Observer" + observer + "was not registered. "); <br/>}< br/> mobservers. remove (INDEX ); <br/>}</P> <p>/** <br/> * remove all registered observer <br/> */<br/> Public void unregisterall () {<br/> synchronized (mobservers) {<br/> mobservers. clear (); <br/>}< br/>}

A detailed description of the observer pattern shows the http://blog.csdn.net/ace1985/article/details/5753658 of this article. OK, let's get down to the point. Let's take a look at our imageadapter. java. The annotations in the above analysis and code should be well understood:

Package COM. google. android. panoramio; </P> <p> Import android. content. context; <br/> Import android. database. datasetobserver; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. baseadapter; <br/> Import android. widget. imageview; <br/> Import android. widget. textview; </P> <p>/** <br/> * adapter used to bind image resources to imagelist <br/> */<br/> public class imageadapter extends baseadapter {< /P> <p>/** <br/> * maintains the state of our data <br/> */<br/> private imagemanager mimagemanager; </P> <p> private context mcontext; </P> <p> private mydatasetobserver mobserver; </P> <p>/** <br/> * imagemanager plays the subject role, instances of this class will be added to the observer list in the imagmanager class <br/> *. When the data in imagelist changes (detected by imagemanager) <br/> * imagemanager will notify you of changes to the mydatasetobserver instance <br/> */<br/> private class mydatasetobserver extends datasetobserver {<br/> @ override <br/> Public void onchanged () {<br/> // baseadapter maintains a datasetobservable object mdatasetobservable <br/> // This function is used to notify all observer data of mdatasetobservable to change <br/> notifydatasetchanged (); <br/>}</P> <p> @ override <br/> Public void oninvalidated () {<br/> // baseadapter maintains a datasetobservable object mdatasetobservable <br/> // This function is used to notify mdatasetobservable that all observer data becomes invalid <br/> yydatasetinvalidated (); <br/>}</P> <p> Public imageadapter (context c) {<br/> mimagemanager = imagemanager. getinstance (c); <br/> mcontext = C; <br/> mobserver = new mydatasetobserver (); </P> <p> mimagemanager. addobserver (mobserver ); // set the mobserver as the mimagemanager observer <br/>}</P> <p>/** <br/> * returns the number of displayed images <br/> * <br/> * @ see android. widget. adapter # getcount () <br/> */<br/> Public int getcount () {<br/> return mimagemanager. size (); <br/>}</P> <p>/** <br/> * returns the image of the specified index <br/> * @ see android. widget. adapter # getitem (INT) <br/> */<br/> Public object getitem (INT position) {<br/> return mimagemanager. get (position ); <br/>}</P> <p>/** <br/> * returns the image ID of the specified index <br/> * @ see android. widget. adapter # getitemid (INT) <br/> */<br/> Public long getitemid (INT position) {<br/> panoramioitem S = mimagemanager. get (position); <br/> return S. GETID (); <br/>}</P> <p>/** <br/> * return the view used to display the image at the specified index. <br/> *@ param position index <br/> * @ Param convertview views that can be reused, it may be null. <br/> * @ Param parent returns the parent view of the view. <br/> * @ return is used to display the view of the image at the specified index. <br/> */<br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> View view; <br/> If (convertview = NULL) {<br/> // create a new view <br/> layoutinflater Inflater = (layoutinflater) mcontext <br/>. getsystemservice (context. layout_inflater_service); <br/> View = Inflater. inflate (R. layout. image_item, null); <br/>}else {<br/> // use an existing view <br/> View = convertview; <br/>}< br/> panoramioitem S = mimagemanager. get (position); </P> <p> imageview I = (imageview) view. findviewbyid (R. id. image); <br/> I. setimagebitmap (S. getbitmap (); // set the bitmap to view. <br/> I. setbackgroundresource (R. drawable. picture_frame); // set the background image of the imageview </P> <p> textview t = (textview) view. findviewbyid (R. id. title); <br/> T. settext (S. gettitle (); // set the image name </P> <p> T = (textview) view. findviewbyid (R. id. owner); <br/> T. settext (S. getowner (); // set the image author <br/> return view; <br/>}</P> <p>}

Similarly, the content of the imagelist. Java file is as follows:

Package COM. google. android. panoramio; </P> <p> Import android. app. listactivity; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. database. datasetobserver; <br/> Import android. OS. bundle; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. view. window; <br/> Import android. widget. listview; </P> <p>/** <br/> * displays the activit of the image list. Y <br/> */<br/> public class imagelist extends listactivity {</P> <p> imagemanager mimagemanager; </P> <p> private mydatasetobserver mobserver = new mydatasetobserver (); </P> <p>/** <br/> * Save the scaling level used by the user to select a search area on the main interface <br/> */<br/> private int mzoom; </P> <p>/** <br/> * Save the central point latitude of the search area selected on the main interface. <br/> */<br/> private int mlatitudee6; </P> <p>/** <br/> * Save the center longitude of the search area selected on the main interface <br/> */<br/> private int mlongitu Dee6; </P> <p>/** <br/> * register as the observer of the imagemanager instance, this function is used to display and close the loading progress on the imagelist interface when imagemanager finishes image download <br/> */<br/> private class mydatasetobserver extends datasetobserver {<br/> @ override <br /> Public void onchanged () {<br/> If (! Mimagemanager. isloading () {<br/> getwindow (). setfeatureint (window. feature_indeterminate_progress, <br/> window. progress_visibility_off); <br/>}</P> <p> @ override <br/> Public void oninvalidated () {<br/>}</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> requestwindowfeature (window. feature_indeterminate_progress); // displays the uncertain progress. <br/> super. oncreate (savedinstancestate); </P> <p> mimagemanager = imagemanager. getinstance (this); </P> <p> // obtain the listview and add copyright information at the bottom. <br/> listview = getlistview (); <br/> layoutinflater Inflater = (layoutinflater) getsystemservice (context. layout_inflater_service); <br/> View footer = Inflater. inflate (R. layout. list_footer, listview, false); <br/> listview. addfooterview (footer, null, false); </P> <p> // set the custom imageadapter to this listactivity <br/> setlistadapter (New imageadapter (this )); </P> <p> // In androidmanifest. in the XML file, theme is set for our list. light, here the background is removed <br/> listview. setbackgrounddrawable (null); <br/> // when imagemanager is still downloading resources, the progress bar is displayed as busy, and register the observer to hide the progress bar when the download ends. <br/> If (mimagemanager. isloading () {<br/> getwindow (). setfeatureint (window. feature_indeterminate_progress, <br/> window. progress_visibility_on); <br/> mimagemanager. addobserver (mobserver ); <br/>}</P> <p> // Save the information about the search area transmitted from the panoramio main interface. <br/> intent I = getintent (); <br/> mzoom = I. getintextra (imagemanager. zoom_extra, integer. min_value); <br/> mlatitudee6 = I. getintextra (imagemanager. latitude_e6_extra, integer. min_value); <br/> mlongitudee6 = I. getintextra (imagemanager. longitude_e6_extra, integer. min_value); <br/>}</P> <p> @ override <br/> protected void onlistitemclick (listview L, view V, int position, long ID) {<br/> panoramioitem item = mimagemanager. get (position); </P> <p> // create an intent to pass the relevant data to the viewimage and display the information of a single image. <br/> intent I = new intent (this, viewimage. class); <br/> I. putextra (imagemanager. panoramio_item_extra, item); <br/> I. putextra (imagemanager. zoom_extra, mzoom); <br/> I. putextra (imagemanager. latitude_e6_extra, mlatitudee6); <br/> I. putextra (imagemanager. longitude_e6_extra, mlongitudee6); <br/> startactivity (I); <br/>}</P> <p>}

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.