[Android] How should context be written?

Source: Internet
Author: User

If you want to bring up an alertdialog, write the followingCode

 
Alertdialog. Builder Alert = new alertdialog. Builder (this); alert. settitle ("warning"); alert. setmessage ("wrong time! "); Alert. Show ();

 


The prototype of the constructor is alertdialog. builder (context Arg) requires an object of the context class as a parameter. Generally, we write this in the activity. Therefore, this indicates that alertdialog is displayed in the current session.

 

In one of myProgramIn, I have customized an interface public interface custompickerlistener. when implementing this interface method, I need to pop up an alertdiener. Here, if this is entered in the parameter table, an error is prompted: the constructor alertdialog. builder (New custompickerlistener () {}) is undefined.

In fact, writing this in this place is obviously wrong, but what do you need to write to achieve the goal?

In the official document, context is interpreted as interface to global information about an application environment. this is an abstract class whose implementation is provided by the Android system. it allows access to application-specific resources and classes, as well as up-callfor application-level operations such as launching activities, broadcasting and processing intents, etc.

So I tried to write my program package. Target class. This, as shown below:

 
Alertdialog. Builder Alert = new alertdialog. Builder (COM. Android. alcoholtest. alcoholtest. This );

And then it succeeds.

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.