The callback function transfers activity data using dialog.

Source: Internet
Author: User

1. Callback Function

The callback function is defined as follows:ProgramC calls function a in S and enters program s through this function. program s executesCodeTo a certain extent, function B in program C will be called in turn. This function B is called a callback function for program c.

Understanding of callback functions: (1) program C does not directly call callback function B in the program, but indirectly calls callback function B through program;

(2) program s must know in advance the prototype of the callback function for convenient calling. Therefore, an interface (interface B) must be agreed in advance to specify the transmitted parameter type and return value type;

(3) callback function B is the implementation of interface B. That is to say, the server program s and interfaces are equivalent to the design of a framework, the client program c implements the callback function B;

(4) callback process: program c sends its program entry address to program s by calling function A. After program s runs, access Program C through the passed entry address and call the internal callback function B. During the call, pass the data in the service program running as the callback function parameter to program c, thus, communication between the service program and the customer program is realized.

2. Application of callback function in Android ---- communication between dialog and Activity

In Android development, a dialog is activated in the activity. If you select an image in the dialog, so we need to pass the path address of the image to the activity to tell the activity which image is selected. In this case, we can use the callback function.

The implementation process is as follows:

(1) first, we need to define an interface that declares the type of the transmission parameter and the return type. Here, the image address is used as a parameter and the type is string:

1 Public InterfaceDialoglistener{2//Interface Function for obtaining the image address callback function3Public VoidGetimageaddr (string ADDR );4}

 

(2) customize a class mydialog to inherit the dialog. In this class, define a method to pass in the callback function address setdialoglistener (equivalent to registering the callback function ), call the callback function getimageaddr where the data needs to be transmitted. The parameter that calls the function is the image address that needs to be passed to the activity.

 Package  Haitun. textchat;  Import Haitun. interface. dialoglistener;  Import  Haitun. pictureinfo. pictureinfo;  Import  Haitun. sdcard. fileutils;  Import  Java. util. arraylist;  Import  Java. util. hashmap;  Import  Java. util. List;  Import  Android. App. Dialog;  Import  Android. content. context; Import  Android. Graphics. Bitmap;  Import  Android. OS. Bundle;  Import  Android. View. view;  Import  Android. widget. adapterview. onitemclicklistener;  Import  Android. widget. adapterview;  Import  Android. widget. gridview;  Import  Android. widget. imageview; Import  Android. widget. simpleadapter;  Import  Android. widget. simpleadapter. viewbinder;  Public   Class   Mydialog   Extends  Dialog {context;Dialoglistener  = NULL;  Public  Mydialog (context ){  Super  (Context ); This . Context = Context ;}  Public Mydialog (context, Int  Theme ){  Super  (Context, theme );  This . Context = Context ;}  /**  * ****** Pass the callback function address to mydialog ********  */      Public   Void Setdialoglistener (dialoglistener listener ){  This  . Dialoglistener =  Listener;}  Private Gridview = Null  ; List <Bitmap> imagelist = Null  ; Simpleadapter Adapter = Null  ; List <Pictureinfo> picinfolist = Null ; @ Override  Protected   Void  Oncreate (bundle savedinstancestate ){  //  Todo auto-generated method stub          Super  . Oncreate (savedinstancestate); setcontentview (R. layout. Picture); gridview = (Gridview) findviewbyid (R. Id. gridview); pictureinfo picinfo = New  Pictureinfo (); fileutils File = New Fileutils (); picinfolist = File. getpicturefiles ("haitun/picture" ); Imagelist = Picinfo. getimagelist (picinfolist); List <Hashmap <string, Object> listimage = New Arraylist  ();  For ( Int I = 0; I <imagelist. Size (); I ++ ) {Hashmap <String, Object> map = New Hashmap <string, Object>(); Map. Put ( "Itemimage" , Imagelist. Get (I); listimage. Add (MAP);} string [] From = New String [] {"itemimage" };  Int [] To = New   Int  [] {R. Id. image_itemimage}; Adapter = New Simpleadapter (mydialog. This  . Context, listimage, R. layout. Cell, from, );  /** ********* Bind images and data ************  */  Adapter. setviewbinder (  New  Viewbinder () {@ override  Public   Boolean  Setviewvalue (view, object data, string textrepresentation ){  //  Todo auto-generated method stub                  If (View Instanceof Imageview & DataInstanceof  Bitmap) {imageview I = (Imageview) view; I. setimagebitmap (Bitmap) data );  Return   True  ;}  Return   False  ;}}); Gridview. setadapter (adapter); gridview. setonitemclicklistener (listener );}  /**  * *********** Select ************  */ Onitemclicklistener listener = New  Onitemclicklistener () {@ override  Public   Void Onitemclick (adapterview <?> Arg0, view arg1, Int  Arg2,  Long  Arg3 ){  //  Todo auto-generated method stub  Dismiss (); string ADDR = Picinfolist. Get (arg2). getpicfilename ();//  Obtain the path address of the image. System. Out. println ("" + ADDR );Dialoglistener. getimageaddr (ADDR );   //  Callback Function for passing image addresses  }};} 

(3) Implement the callback function interface in the activity, and pass the implemented anonymous internal class as a parameter to the setdilaloglistener method. After this interface is implemented, the internal method will monitor whether the dialog method is called. When mydialog calls this method, the image address will be transmitted, and execute the content in the callback function.

 Mydialog Dialog = New   Mydialog (Chatactivity. This ); Dialog.Setdialoglistener (dialoglistener );Dialog. Show ();Dialoglistener Dialoglistener = New  Dialoglistener() {@ Override  Public   Void  Getimageaddr (string ADDR){  //  Todo auto-generated method stub  // Wait until it is called 
// The ADDR string passed in as a parameter is the image address.
Toast. maketext (chatactivity. This , "" + ADDR, Toast. length_short). Show (); Bitmap bitmap = Bitmapfactory. decodefile (ADDR); listviewupdate (config. GetUserName () + ":" , Bitmap );}};

 

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.