Listener Listener Mode

Source: Internet
Author: User

For an example of how to use the Listener Listener mode:

Background: In one activity there is a way to get a dialog up, which is the "Loading" dialog box, in which the system is dealing with something,

But the result is in another manager, but canceling the dialog display is only available in my activity, and the question is: How to get the manager to the results,

Send me a message to cancel the display of dialog in the activity.

Let's use the listener mode to implement it.

First, I need to cancel the dialog display, if the method is DismissDialog (), and this method must be used somewhere in the activity.

Then we can. This method is placed in the listener implementation method, which is equivalent to the Click () method in the Onclicklistener.

You must have the following code snippet in your activity:

Accountmanager Maccountmanager = Accountmanager.getinstance ();
    Maccountmanager.seterroroccurlistener (New Onerroroccurlistener () {
	@Override public
	void Onerroroccur () {
	   DismissDialog (dialog_loading_cancelable);
	}
});

Second: From the above code can be seen, there is a interface called Onerroroccurlistener, he has a method is not implemented called onerroroccur (), so we need to write a interface interface,

It doesn't matter where you put it, but it must be public. As follows:

	Public interface onerroroccurlistener{public
		void Onerroroccur ();
	}
Again: As can be seen in the above code, the manager has a Seterroroccurlistener (Onerroroccurlistenr listener) method, so in the manager to add

	public void Seterroroccurlistener (Onerroroccurlistener listener) {
		This.mdialoglistener = listener;
	}
	
Also set the attribute Mdialoglistener in the manager as a global variable.


Finally: and the most important question is: how to trigger the activity of DismissDialog () to execute it.

In the result method in your manager class, add

		public void result (int error) {
			if (mdialoglistener! = null) {
				mdialoglistener.onerroroccur ();
			}
			Toast.maketext (Healthapplication.getcontext (), error, Toast.length_long). Show ();
		



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.