Implemented contentprovider query functionality.
This commit is contained in:
parent
3da88509ff
commit
9614ae2020
|
@ -1,5 +1,7 @@
|
|||
package no.nilsnh.uibevents;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.MatrixCursor;
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
@ -63,5 +65,18 @@ public class ApplicationTest extends AndroidTestCase {
|
|||
assertTrue("Nothing have been deleted", deletedRows > 0);
|
||||
}
|
||||
|
||||
public void testGetCursor() {
|
||||
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());
|
||||
Cursor cursor = getContext().getContentResolver()
|
||||
.query(EventContract.EventEntry.CONTENT_URI, null, null, null, null);
|
||||
assertNotNull(cursor);
|
||||
|
||||
}
|
||||
}
|
|
@ -67,6 +67,31 @@ public class Event {
|
|||
return cValues;
|
||||
}
|
||||
|
||||
public static String[] getKeysAsStringList() {
|
||||
return new String[]{
|
||||
"id",
|
||||
"type",
|
||||
"title",
|
||||
"date_from",
|
||||
"date_to",
|
||||
"location",
|
||||
"details",
|
||||
"url"};
|
||||
}
|
||||
|
||||
public String[] getValuesAsStringList() {
|
||||
return new String[]{
|
||||
id,
|
||||
type,
|
||||
title,
|
||||
dateFrom,
|
||||
dateTo,
|
||||
location,
|
||||
details,
|
||||
url
|
||||
};
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package no.nilsnh.uibevents.data;
|
|||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.MatrixCursor;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONArray;
|
||||
|
@ -11,7 +12,6 @@ import org.json.JSONObject;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
@ -106,7 +106,6 @@ public class EventDbHelper {
|
|||
}
|
||||
|
||||
public void saveFile(String data) {
|
||||
FileOutputStream outputStream;
|
||||
try {
|
||||
String fullFilePath = ctx.getFilesDir() + "/" + filename;
|
||||
BufferedWriter buf = new BufferedWriter(new FileWriter(fullFilePath));
|
||||
|
@ -180,7 +179,7 @@ public class EventDbHelper {
|
|||
Integer numDeletedRows = null;
|
||||
|
||||
//If everything is to be deleted just delete the file
|
||||
if (selection == "1") {
|
||||
if (selection == null) {
|
||||
numDeletedRows = storedEvents.size();
|
||||
File file = new File(ctx.getFilesDir() + "/" + filename);
|
||||
file.delete();
|
||||
|
@ -199,4 +198,13 @@ public class EventDbHelper {
|
|||
|
||||
return numDeletedRows;
|
||||
}
|
||||
|
||||
public MatrixCursor query() {
|
||||
ArrayList<Event> storedEvents = getStoredData();
|
||||
MatrixCursor mCursor = new MatrixCursor(Event.getKeysAsStringList());
|
||||
for (Event event: storedEvents) {
|
||||
mCursor.addRow(event.getValuesAsStringList());
|
||||
}
|
||||
return mCursor;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.content.ContentProvider;
|
|||
import android.content.ContentValues;
|
||||
import android.content.UriMatcher;
|
||||
import android.database.Cursor;
|
||||
import android.database.MatrixCursor;
|
||||
import android.net.Uri;
|
||||
|
||||
public class EventProvider extends ContentProvider {
|
||||
|
@ -20,7 +21,18 @@ public class EventProvider extends ContentProvider {
|
|||
|
||||
@Override
|
||||
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
|
||||
return null;
|
||||
MatrixCursor mCursor;
|
||||
switch (sUriMatcher.match(uri)) {
|
||||
// "weather/*/*"
|
||||
case EVENT: {
|
||||
mCursor = eventDbHelper.query();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unknown uri: " + uri);
|
||||
}
|
||||
mCursor.setNotificationUri(getContext().getContentResolver(), uri);
|
||||
return mCursor;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -56,11 +68,8 @@ public class EventProvider extends ContentProvider {
|
|||
|
||||
@Override
|
||||
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
||||
|
||||
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(
|
||||
|
|
Loading…
Reference in a new issue