Android AlertDialog

  public void actionAlertDialog1(){
        ArrayList<txtpath> list = initData();
        AlertDialog.Builder builder;
        AlertDialog alertDialog;
        Context context = MainActivity.this;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        //用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件
        View layout = inflater.inflate(R.layout.item_listview, (ViewGroup)findViewById(R.id.layout_myview));
        // 然后调用inflate方法将xml布局文件转成View
        ListView myListView = (ListView) layout.findViewById(R.id.mylistview);
        //
         MyAdapter adapter = new MyAdapter(context, list);
//捆绑数据
//decode decode =new decode(adapter.res); // ECGlist = decode.stand(); myListView.setAdapter(adapter); builder = new AlertDialog.Builder(context).setView(layout); // builder = new AlertDialog.Builder(MainActivity.this); // builder.setView(layout); alertDialog = builder.create(); alertDialog.show(); }
关键在于这个Context context = MainActivity.this;中的context怎么写
如果想要弹出一个AlertDialog,要写如下的代码


AlertDialog.Builder alert =new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("Wrong time!");
alert.show();
 


这里构造方法的原型是AlertDialog.Builder(Context arg) 需要一个Context类的对象作为参数,一般我们都在Activity里写,所以用this,表示在当前的会话中弹出AlertDialog。

 


在我的一个程序里,我自定义了一个接口Public interface CustomPickerListener,在实现这个接口的方法时我需要弹出来一个AlertDialog,这里,参数表里填写this的话会提示错误:The constructor AlertDialog.Builder(new CustomPickerListener(){}) is undefined.


其实这个地方写this很明显是错误的,但是要写什么才能达成目的呢?


官方文档上对context的解释是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-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.


于是我就试着写上了我程序的 包.目标类.this ,如下

1
AlertDialog.Builder alert =new AlertDialog.Builder(com.android.alcoholtest.AlcoholTest.this);
然后就成功了
主要原因在与

An Activity inherits a Context. AlertDialog.Builder specifies a Context argument because it can then be used by ANY class that is a subclass of Context, including an Activity, ListActivity, Service, ... (There is a common coding idiom behind this - you can learn more about it by reading Item I8 (on Interfaces and Abstract classes) in Joshua Bloch's fantastic Effective Java).

getApplicationContext() returns the context for your application, which is mostly the same as your activities context - and the "mostly" is what is throwing you off. The details are unclear but this is a widely encountered issue, and the typical answer is to use the context that will be writing the alert to the screen. Note that that is not the one returned by getApplicationContext().

Now if you're like me, you may say "but I am working in a class that does not inherit from Activity - which is why I want to use getApplicationContext() for this in the first place - duh!" I'm actually don't speak as rudely as that ;p .. the point was I've been here too. I fixed it like so: 1) ask yourself "do I have my UI AlertDialog code in a non-activity class because I want to share it across activities .. or even across ListActivities, Services, ...?". If not, hmmm... do you really have AlertDialog UI calls in code that you can't guarantee will have access to the UI (and thus context)? If so, reconsider your design.

Presuming you do want to share this class across Activities, ... the answer becomes clear. You want your class to be usable by a variety of callers, each probably with its own context: so the caller must pass its context into your class as an argument:

myClass(Context theContext, ...) { ... }

Each activity, service, etc. then makes calls like so:

myClass(this, ...);

Look familiar?

Do be careful! that if you are sharing code, you must consider the possibility of different calls coming into your shared code in parallel, with all the many ramifications. Thats beyond our scope here...

Have fun :)

原文地址:https://www.cnblogs.com/puck/p/4208494.html