开发者

Custom AlertDialog not showing

I have a TextViewer Activity , that has a button, and when I click on it I want to popup an AlertDialog with list. I followed this link but it does not work (no popup). I believe the context is wrong. I used the following code:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resources);
    ImageButton btnlist = (ImageButton)findViewById(R.id.list);
    btnlist.setOnClickListener(new View.OnClickListener() {
             public void onClick (View v){                  

              if (Vars.bookchapter>1){
               final CharSequence[] items = {"Red", "Green", "Blue"};
               Context mContext = getBaseContext();
               AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
               builder.setTitle(开发者_Python百科"Pick a color");
               builder.setItems(items, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int item) {
                       Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                   }
               });
               AlertDialog alert = builder.create();
              }else{
               //Nothing
              }
             }});         
     }
        }


You haven't called the show() method. Do this:

AlertDialog alert = builder.create();
alert.show();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜