From 74711185509a77a1428ef04194fcf764880147e2 Mon Sep 17 00:00:00 2001 From: agk512 Date: Thu, 21 Nov 2013 15:52:23 -0500 Subject: [PATCH] Tap to delete alerts built. --- src/com/uncc/gameday/activities/Home.java | 68 +++++++++++++---------- src/com/uncc/gameday/alerts/Alert.java | 4 ++ src/com/uncc/gameday/alerts/AlertDB.java | 10 ++++ 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/com/uncc/gameday/activities/Home.java b/src/com/uncc/gameday/activities/Home.java index 21ac49f..a0152bb 100644 --- a/src/com/uncc/gameday/activities/Home.java +++ b/src/com/uncc/gameday/activities/Home.java @@ -9,6 +9,8 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; @@ -25,9 +27,6 @@ import com.uncc.gameday.alerts.AlertService; * The Class Home. */ public class Home extends MenuActivity { - - - /** The alarm rate. */ private final long alarmRate = 300000; // 5 Minutes @@ -47,7 +46,6 @@ public class Home extends MenuActivity { } }); - // Start up the AlarmManager to fetch alerts in the background AlarmManager am = (AlarmManager) this.getSystemService(ALARM_SERVICE); Intent alertFetcher = new Intent(this, AlertService.class); @@ -63,43 +61,53 @@ public class Home extends MenuActivity { this.displayList(); } - + //displays all alerts in database, regardless of being shown or not public void displayList() { - List alerts = new AlertDB(this).fetchAll(); + final List alerts = new AlertDB(this).fetchAll(); - String[] printArray = new String[alerts.size()]; - - //get message from each alert and put in printArray - for(int i = 0; i < alerts.size(); i++) - { - printArray[i] = alerts.get(i).getMessage(); - } - - ListView listView = (ListView)findViewById(R.id.alertsListView); - ArrayAdapter adapter = - new ArrayAdapter(this,android.R.layout.simple_list_item_1, printArray); + final ListView listView = (ListView)findViewById(R.id.alertsListView); + final ArrayAdapter adapter = + new ArrayAdapter(this,android.R.layout.simple_list_item_1, alerts); listView.setAdapter(adapter); + + //tap to delete alert from list and database + listView.setOnItemClickListener(new OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, + long id) { + deleteAlert(alerts.get(position)); + displayList(); + } + }); } + //function to delete alert from database + public void deleteAlert(Alert alert) + { + new AlertDB(this).deleteAlert(alert); + } + + //displays only unread alerts public void displayUnread() { + final List alerts = new AlertDB(this).fetchUnread(); - List alerts = new AlertDB(this).fetchUnread(); - - String[] printArray = new String[alerts.size()]; - - //get message from each alert and put in printArray - for(int i = 0; i < alerts.size(); i++) - { - printArray[i] = alerts.get(i).getMessage(); - } - - ListView listView = (ListView)findViewById(R.id.alertsListView); - ArrayAdapter adapter = - new ArrayAdapter(this,android.R.layout.simple_list_item_1, printArray); + final ListView listView = (ListView)findViewById(R.id.alertsListView); + final ArrayAdapter adapter = + new ArrayAdapter(this,android.R.layout.simple_list_item_1, alerts); listView.setAdapter(adapter); + + //tap to delete alert from list and database + listView.setOnItemClickListener(new OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, + long id) { + deleteAlert(alerts.get(position)); + displayList(); + } + }); } /** diff --git a/src/com/uncc/gameday/alerts/Alert.java b/src/com/uncc/gameday/alerts/Alert.java index 8b2d4c6..0362efe 100644 --- a/src/com/uncc/gameday/alerts/Alert.java +++ b/src/com/uncc/gameday/alerts/Alert.java @@ -55,6 +55,10 @@ public class Alert { this.shown = i; } + public String toString() { + return this.message; + } + public void displayNotification(Context ctx) { NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx) .setSmallIcon(R.drawable.ic_launcher) diff --git a/src/com/uncc/gameday/alerts/AlertDB.java b/src/com/uncc/gameday/alerts/AlertDB.java index 965f89e..e62a031 100644 --- a/src/com/uncc/gameday/alerts/AlertDB.java +++ b/src/com/uncc/gameday/alerts/AlertDB.java @@ -83,6 +83,16 @@ public class AlertDB extends SQLiteOpenHelper { return null; } + //delete specified alert from DB + public void deleteAlert(Alert alert) { + SQLiteDatabase db = this.getWritableDatabase(); + db.delete(TABLE_ALERTS, KEY_MESSAGE + " = ?", + new String[] { String.valueOf(alert.getMessage()) }); + db.close(); + + } + + //get all alerts from Database //regardless of type, or if it has been shown