Data display for updating activity in dialog

Source: Internet
Author: User

Let's say that there is a button and a textview in the Activity,activity, click the buttons, Pop dialog, a ListView in the dialog box, select an item in the ListView, close the dialog box, Update the value of EditText in activity to the value of the item you selected. To analyze this problem, assuming that dialog is Alertdialog, you can create dialog directly in the Onclicklistener event of the button, Update the EditText value in Alertdialog's Setpositivebutton or other button listener, and if dialog is a custom dialog, you can also fully implement this custom dialog in the activity's inner class So that you can still access the EditText in the selection event listener of the ListView in dialog. But I don't like it in this way, whether it's the code complexity or the reason for the coupling, anyway, I prefer to use a separate class to create the custom dialog. In this case, you will not have access to the activity's EditText in dialog unless you set the EditText to public (which is certainly not a good way). On-line search, found can be implemented by the callback function, still use the previous article used in the custom dialog box as an example, the following steps: 1. Create a new interface as the listener for dialog and declare the callback function in the interface:
   /* *     * Customize Dialog Listener     * @author kael.chen     *     * */public    interface  Prioritylistener {        /* * *         callback function to refresh activity UI display         after dialog listener event trigger  *        /publicvoid  Refreshpriorityui ();    }

2. Add a constructor with listener parameters for custom dialog:

PrivatePrioritylistener Listener;  PublicPrioritydlg (Context context) {super (context);  This. Context =context; //TODO auto-generated Constructor stub    }         PublicPrioritydlg (Context context,inttheme)        {Super (context, theme);  This. Context =context; }         PublicPrioritydlg (Context context,inttheme, Prioritylistener Listener) {         This(context, theme);  This. Listener =Listener; }

3, in the dialog need to call the callback function, such as when the ListView selection event triggered:

Dlg_priority_lvw.setonitemclicklistener (new  Adapterview.onitemclicklistener () {                    @Override                     publicvoid Onitemclick (adapterview<?> arg0, View arg1,                            int  Long  arg3) {                                    dismiss ();                        Listener.refreshpriorityui ();                    }                });

4. Then you use the constructor with the listener parameter in the activity to instantiate the custom dialog, and implement the callback function declared in the listener:

   //add listeners for priority selection buttonsTask_simple_form_priority.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//pop-up Task Priority selection dialog boxPrioritydlg dlg =NewPrioritydlg (simpletaskactivity. This, R.style.dlg_priority,NewPrioritydlg.prioritylistener () {@Override Public voidRefreshpriorityui () {//This is what the UI for refreshing the activity shows, and if you need to use the data returned from dialog, you can either store the data in a global variable or pass it as a parameter to the callback function.Toast.maketext (simpletaskactivity. This,"Complete the selection", Toast.length_short). Show ();                }                });                        Dlg.show (); }        });

Data display for updating activity in dialog

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.