3

我需要创建一个带有两个按钮的警报对话框作为视图。我创建了它,但问题是在单击其中一个视图时执行的操作后我无法关闭它。这意味着在第一个按钮中

这就是我要找的

前两个按钮是两个视图,我将它们粘贴在另一个视图中是线性布局

我的覆盖类

public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
       DatabaseHandler db;
       private Context context;
       double lat;
       double lon;
    // Alert Dialog Manager
    AlertDialogManager alert = new AlertDialogManager();

       public AddItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
       }

       public AddItemizedOverlay(Drawable defaultMarker, Context context) {
            this(defaultMarker);
            this.context = context;
       }

       @Override
       public boolean onTouchEvent(MotionEvent event, MapView mapView)
       {   

           if (event.getAction() == 1) {
               GeoPoint geopoint = mapView.getProjection().fromPixels(
                   (int) event.getX(),
                   (int) event.getY());
               db = new DatabaseHandler(context);
               // latitude & longtitude (double)
               lat = geopoint.getLatitudeE6() / 1E6;
               lon = geopoint.getLongitudeE6() / 1E6;


           }
        return false;
       } 

       @Override
       protected OverlayItem createItem(int i) {
          return mapOverlays.get(i);
       }

       @Override
       public int size() {
          return mapOverlays.size();
       }

       @Override
       protected boolean onTap(int index) {
         OverlayItem item = mapOverlays.get(index);
         final String  title= item.getTitle();
         final String snippet= item.getSnippet();

         if(title.equalsIgnoreCase("Your Location")){

             alert.showpickAlertDialog1(context, title, snippet);

         }
         else if(title.equalsIgnoreCase("0")||title.equalsIgnoreCase("2")||title.equalsIgnoreCase("3")){
             String lati = String.valueOf(lat);
             String longi = String.valueOf(lon);        
alert.showpickAlertDialog2(context, lati, longi, snippet);
         }
         else if(title.equalsIgnoreCase("1")||title.equalsIgnoreCase("4")||title.equalsIgnoreCase("5")||title.equalsIgnoreCase("6")){

         }
        return true;

       }

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

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

    }

自定义的 AlertDialogManager 类

public class AlertDialogManager {
    private Context context;
    DatabaseHandler db = new DatabaseHandler(context);
    public void showAlertDialog(Context context, String title, String message,
            Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        if(status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                System.exit(0);
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    public void showpickAlertDialog2(final Context context, final String lat, final String lon,final String snippet) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        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() {

            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 = lat;
                   String longtitude = 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);


               }
    park_button.setEnabled(false);

            }
        });


        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);
            }
        });

        alertDialog.setView(layout);
        alertDialog.setButton("Back", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }
    public void showpickAlertDialog1(Context context, String title, String message) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        alertDialog.setButton("Back", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });

        // Showing Alert Message
        alertDialog.show();


}
}
4

1 回答 1

2

您当前正在将空上下文传递给DatabaseHandlerAlertDialogManager 类中的类构造函数:

public class AlertDialogManager {
    private Context context;
    DatabaseHandler db ;
    public void showAlertDialog(Context context, String title, String message,
            Boolean status) {
         this.context=context;
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        db = new DatabaseHandler(context);

   //..your code here...

并解雇 Dialog 使用alertDialog.dismiss()而不是System.exit(0);

于 2013-03-08T07:43:31.213 回答