Is now able to delete.
This commit is contained in:
parent
aee69453c4
commit
3da88509ff
|
@ -47,4 +47,21 @@ public class ApplicationTest extends AndroidTestCase {
|
|||
.insert(EventContract.EventEntry.CONTENT_URI, events.get(0).getContentValues());
|
||||
assertTrue(eventDbHelper.getStoredData().contains(events.get(0)));
|
||||
}
|
||||
|
||||
public void testDeleteAll() {
|
||||
ArrayList<Event> events = null;
|
||||
try {
|
||||
events = EventDbHelper.parseJsonEvents(TestUtils.getExampleJson());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
getContext().getContentResolver()
|
||||
.insert(EventContract.EventEntry.CONTENT_URI, events.get(0).getContentValues());
|
||||
assertTrue(eventDbHelper.getStoredData().contains(events.get(0)));
|
||||
Integer deletedRows = getContext().getContentResolver()
|
||||
.delete(EventContract.EventEntry.CONTENT_URI, null, null);
|
||||
assertTrue("Nothing have been deleted", deletedRows > 0);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -143,4 +143,12 @@ public class Event {
|
|||
result = 31 * result + (url != null ? url.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean isToBeDeleted(String selection, String[] selectionArgs) {
|
||||
Boolean isToBeDeleted = false;
|
||||
for (String arg : selectionArgs) {
|
||||
if (getContentValues().getAsString(selection).equals(arg)) isToBeDeleted = true;
|
||||
}
|
||||
return isToBeDeleted;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,9 +108,10 @@ public class EventDbHelper {
|
|||
public void saveFile(String data) {
|
||||
FileOutputStream outputStream;
|
||||
try {
|
||||
outputStream = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
|
||||
outputStream.write(data.getBytes());
|
||||
outputStream.close();
|
||||
String fullFilePath = ctx.getFilesDir() + "/" + filename;
|
||||
BufferedWriter buf = new BufferedWriter(new FileWriter(fullFilePath));
|
||||
buf.write(data);
|
||||
buf.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
|
@ -118,6 +119,14 @@ public class EventDbHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public void saveFile(ArrayList<Event> events) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Event event: events) {
|
||||
sb.append(event.toString() + "\n");
|
||||
}
|
||||
saveFile(sb.toString());
|
||||
}
|
||||
|
||||
public ArrayList<Event> getStoredData() {
|
||||
File file = new File(ctx.getFilesDir() + "/" + filename);
|
||||
|
||||
|
@ -147,35 +156,47 @@ public class EventDbHelper {
|
|||
}
|
||||
|
||||
public Long insert(ContentValues values) {
|
||||
File file = new File(ctx.getFilesDir(),filename);
|
||||
Event newEvent = new Event(values);
|
||||
|
||||
//First retrieve all data,then check if the value is already stored.
|
||||
ArrayList<Event> storedValues = getStoredData();
|
||||
ArrayList<Event> storedEvents = getStoredData();
|
||||
|
||||
//If no data exist already create new arrayList
|
||||
if (storedValues == null) storedValues = new ArrayList<Event>();
|
||||
if (storedEvents == null) storedEvents = new ArrayList<Event>();
|
||||
|
||||
//If the new data is already there stop execution.
|
||||
if (storedValues.contains(newEvent)) {
|
||||
return Long.valueOf(storedValues.indexOf(newEvent) + 1);
|
||||
}
|
||||
|
||||
storedValues.add(newEvent);
|
||||
Long storedDataPosition = Long.valueOf(storedValues.indexOf(newEvent) + 1);
|
||||
|
||||
//Write new data to textFile
|
||||
try {
|
||||
if (!file.exists()) file.createNewFile();
|
||||
BufferedWriter buf = new BufferedWriter(new FileWriter(ctx.getFilesDir() + "/" + filename, true));
|
||||
for (Event event : storedValues) {
|
||||
buf.write(event.toString());
|
||||
buf.newLine();
|
||||
}
|
||||
buf.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
if (storedEvents.contains(newEvent)) {
|
||||
return Long.valueOf(storedEvents.indexOf(newEvent) + 1);
|
||||
}
|
||||
storedEvents.add(newEvent);
|
||||
Long storedDataPosition = Long.valueOf(storedEvents.indexOf(newEvent) + 1);
|
||||
saveFile(storedEvents);
|
||||
return storedDataPosition;
|
||||
}
|
||||
|
||||
public int delete(String tableName, String selection, String[] selectionArgs) {
|
||||
ArrayList<Event> storedEvents = getStoredData();
|
||||
ArrayList<Event> entriesToDelete = new ArrayList<>();
|
||||
Integer numDeletedRows = null;
|
||||
|
||||
//If everything is to be deleted just delete the file
|
||||
if (selection == "1") {
|
||||
numDeletedRows = storedEvents.size();
|
||||
File file = new File(ctx.getFilesDir() + "/" + filename);
|
||||
file.delete();
|
||||
return numDeletedRows;
|
||||
}
|
||||
|
||||
//Loop through removing any events satisfying the criteria
|
||||
for (Event event : storedEvents) {
|
||||
if(event.isToBeDeleted(selection, selectionArgs)) {
|
||||
storedEvents.remove(event);
|
||||
numDeletedRows = numDeletedRows + 1;
|
||||
}
|
||||
}
|
||||
|
||||
saveFile(storedEvents);
|
||||
|
||||
return numDeletedRows;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,24 @@ public class EventProvider extends ContentProvider {
|
|||
|
||||
@Override
|
||||
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
||||
return 0;
|
||||
|
||||
final int match = sUriMatcher.match(uri);
|
||||
int rowsDeleted;
|
||||
// this makes delete all rows return the number of rows deleted
|
||||
if ( null == selection ) selection = "1";
|
||||
switch (match) {
|
||||
case EVENT:
|
||||
rowsDeleted = eventDbHelper.delete(
|
||||
EventContract.EventEntry.TABLE_NAME, selection, selectionArgs);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unknown uri: " + uri);
|
||||
}
|
||||
// Because a null deletes all rows
|
||||
if (rowsDeleted != 0) {
|
||||
getContext().getContentResolver().notifyChange(uri, null);
|
||||
}
|
||||
return rowsDeleted;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue