2

我在关闭自定义的警报对话框时遇到问题。那就是它有一个包含两个按钮的自定义视图,一个用于数据库操作,另一个用于加载另一个活动。我想在执行数据库操作后关闭对话框。但是现在我不能声明任何命令,或者dialog.dismiss()请有人帮我解决这个问题。cancel()finish()

我的课

  @Override
   protected boolean onTap(int index) {
     OverlayItem item = mapOverlays.get(index);
     final String  title= item.getTitle();
     final String snippet= item.getSnippet();
     AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
     if(title.equalsIgnoreCase("Your Location")){
     dialog.setTitle(item.getTitle());
     dialog.setMessage(item.getSnippet());
     dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
     }
     else if(title.equalsIgnoreCase("")){
     LinearLayout layout = new LinearLayout(context);
     layout.setOrientation(LinearLayout.VERTICAL);

     final Button park_button = new Button(context);
     park_button.setHint("Park here");
   //  park_button.setBackgroundResource();
     layout.addView(park_button);
     park_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("parkbutton:Clicked");
            // Delete DB Values
            int count = db.getDatasCount();
            for(int i = 0;i<count;i++){
                db.deleteValues(i);
            }
            //latitude & longtitude (string)
               String latitude = String.valueOf(lat);
               String longtitude = String.valueOf(lon);
               Log.d("Insert: ", "Inserting ..");
               db.addData(new Datas(latitude, longtitude));
              // Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();

            // Reading DB Data
               Log.d("Reading: ", "Reading all Values...");
               List<Datas> datas = db.getAllDatas();       

               for (Datas dat : datas) {
                   String log = "Id: "+dat.getID()+" ,Latitude: " + dat.getlat() + " ,Longtitude: " + dat.getlon();
                       // Writing DB data to log
               Log.d("DB Data: ", log);
           }
            Intent f = new Intent(context,MainTabActivity.class);
            context.startActivity(f);
        }
    });


     final Button know_more_button = new Button(context);
     know_more_button.setHint("Know more");
     //  park_button.setBackgroundResource();
     layout.addView(know_more_button);
     know_more_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SharedPreferences prefs = context.getSharedPreferences("myprefs", 0);
            SharedPreferences.Editor editor =prefs.edit();
            editor.putString("KEY_REFERENCE", snippet);
            editor.commit();                

            Intent intent = new Intent(context, SinglePlaceActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });

     dialog.setView(layout);
     dialog.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
     }
     dialog.show();
     return true;
   }

   public void addOverlay(OverlayItem overlay) {
      mapOverlays.add(overlay);
   }

   public void populateNow(){
       this.populate();
   }

}
4

3 回答 3

4

首先,为了解释如何获得您想要的行为,我需要您将当前名为dialog的 AlertDialog.Builder 实例重命名为builder。它没有准确命名,将帮助您了解我解决问题的方法。所以这一行:

AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);

会变成:

AlertDialog.Builder builder = new AlertDialog.Builder(this.context);

从那里,您需要:

  1. 创建对调用 show() 时返回给您的 AlertDialog 实例的最终引用 - 并将其称为 dialog
  2. 接下来,将您设置的按钮单击侦听器移到您在构建器上调用 show() 的位置下方。
  3. 最后,引用我让你创建的对话框变量,然后调用dismiss()。

请参见下面的代码:

// ... original code here (right before the end of your onTap main method)
final AlertDialog dialog = builder.show(); // this line used to be "dialog.show();"
know_more_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // ... original code here
        dialog.dismiss();
    }
});
// ... for the other button, do the same
return true;
于 2013-06-14T18:07:52.530 回答
1

如果可以接受,请尝试扩展 DialogFragment(您需要从 FragmentActivity 扩展您的活动)。

public static class Dialog extends DialogFragment {

    @Override
    public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder b = new AlertDialog.Builder(getActivity());

        Button btn = new Button(getActivity());
        btn.setText("dismiss");

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        b.setView(btn);
        b.setPositiveButton("OK", null);
        return b.create();
    }
}

您可以通过这种方式显示对话框(使用 android.support.v4):

new Dialog().show(getSupportFragmentManager(), "dialog");

您还需要在对话和活动之间建立通信。

于 2013-03-04T12:04:16.853 回答
0

做这个

final AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);

不是 AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);

于 2013-03-04T11:13:08.390 回答