androidandroid-alertdialogandroid-custom-viewandroid-overlay

I cannot dismiss the customized alertdialog box


I need to create an alertdialog box with two buttons in it as a view. And i created it but the problem is i cannot dismiss it after the action performed when clicking on one of the views.That means in first button

this is what i am looking for

The first two buttons are two views and i sticked them in another view is a linear layout

My overlay class

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

    }

Customized AlertDialogManager class

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


}
}

Solution

  • you are currently passing null context to DatabaseHandler class constructor in AlertDialogManager class :

    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...
    

    and to dismiss Dialog use alertDialog.dismiss() instead of System.exit(0);