I’ve just been digging around in some of my old projects and found my Hours Manager application.
The purpose of the application is to provide me with a way of logging the hours that I do, given that my schedule is so busy, trying to remember when I did what can often be challenging. Unfortunately it never saw the light of day again due to my hectic schedule.
Given that again I am in a situation where having a log of my work schedule would be beneficial, of course you may be wondering, why don’t I just download one of the many good time management apps for the play store? The answer for me is simple, what would I learn from that?
Programming in Java is something new for me and I have various other android app’s that i have developed in the past although none of which have ever been polished enough to publish. Therefore I’m making a real effort to get this app on the market, not for profit just for the whole experience of releasing an application.
The challenge I have overcome today was adding the functionality to allow users to delete entries from the sql database. To do so I wanted a dialog window to pop up when the user long pressed one of the entries in the list view.
The code that follows simply creates a new Alert Dialog window from within the SQL View fragment, from there two buttons are initialized (one to confirm and one to cancel). The database is also queried to ensure that the correct item is successfully removed, then the array adapter is notified that the data has changed. From there the list view adapter is updated with a new incarnation of the database.
Hopefully this snippet help’s someone else who’s struggling with creating a Alert Dialog within a Fragment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
protected void removeRow(final long _position) { // Create a new Alert Dialog, with the fragment activity context AlertDialog.Builder alert = new AlertDialog.Builder( getActivity() ); // Here we set the dialog title to that of a static string ("Delete Record?") alert.setTitle(R.string.dialog_title); // Now we open the database and query it for the current entry string - Date , Start Time, End Time etc openDatabase(); alert.setMessage("Are you sure you want to remove the following entry?n" + info.getRowInfo(_position)); closeDatabase(); // When the confirm button is pressed alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // Open the database again openDatabase(); // Delete the entry at a specific position info.deleteRow(_position); // Tell the adapter things have changed adapter.notifyDataSetChanged(); adapter.notifyDataSetInvalidated(); // Re-populate our information in the List Adapter populateList(); lv.setAdapter(adapter); } }); // If the cancel button is pressed simply dismiss alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }); // Ensure our database is closed closeDatabase(); // Finally show the alert dialog alert.show(); } |
Alert Dialog displaying record information |