DataContract.java
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 |
package com.ContentProviders.StringContentProvider; import android.content.ContentResolver; import android.net.Uri; // Contract Class for accessing ContentResolver public final class DataContract { public static final String _ID = "_id"; public static final String DATA = "data"; public static final String DATA_TABLE = "data_table"; private static final Uri BASE_URI = Uri .parse("content://com.ContentProviders.StringContentProvider/"); // The URI for this table. public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_URI, DATA_TABLE); // Mime type for a directory of data items public static final String CONTENT_DIR_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/StringContentProvider.data.text"; // Mime type for a single data item public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/StringContentProvider.data.text"; // All columns of this table public static final String[] ALL_COLUMNS = { _ID, DATA }; } |
DataRecord.java
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 |
package com.ContentProviders.StringContentProvider; class DataRecord { private static int id; // Unique ID private final int _id; // Display Name private final String _data; DataRecord(String _data) { this._data = _data; this._id = id++; } String getData() { return _data; } int getID() { return _id; } } |
StringsContentProvider.java
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
package com.ContentProviders.StringContentProvider; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; import android.util.SparseArray; // Note: Currently, this data does not persist across device reboot public class StringsContentProvider extends ContentProvider { // Data storage private static final SparseArray<DataRecord> db = new SparseArray<DataRecord>(); @SuppressWarnings("unused") private static final String TAG = "StringsContentProvider"; // Delete some or all data items @Override public synchronized int delete(Uri uri, String selection, String[] selectionArgs) { int numRecordsRemoved = 0; // If last segment is the table name, delete all data items if (isTableUri(uri)) { numRecordsRemoved = db.size(); db.clear(); // If last segment is the digit, delete data item with that ID } else if (isItemUri(uri)) { Integer requestId = Integer.parseInt(uri.getLastPathSegment()); if (null != db.get(requestId)) { db.remove(requestId); numRecordsRemoved++; } } //return number of items deleted return numRecordsRemoved; } // Return MIME type for given uri @Override public synchronized String getType(Uri uri) { String contentType = DataContract.CONTENT_ITEM_TYPE; if (isTableUri(uri)) { contentType = DataContract.CONTENT_DIR_TYPE; } return contentType; } // Insert specified value into ContentProvider @Override public synchronized Uri insert(Uri uri, ContentValues value) { if (value.containsKey(DataContract.DATA)) { DataRecord dataRecord = new DataRecord(value.getAsString(DataContract.DATA)); db.put(dataRecord.getID(), dataRecord); // return Uri associated with newly-added data item return Uri.withAppendedPath(DataContract.CONTENT_URI, String.valueOf(dataRecord.getID())); } return null; } // return all or some rows from ContentProvider based on specified Uri // all other parameters are ignored @Override public synchronized Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Create simple cursor MatrixCursor cursor = new MatrixCursor(DataContract.ALL_COLUMNS); if (isTableUri(uri)) { // Add all rows to cursor for (int idx = 0; idx < db.size(); idx++) { DataRecord dataRecord = db.get(db.keyAt(idx)); cursor.addRow(new Object[] { dataRecord.getID(), dataRecord.getData() }); } } else if (isItemUri(uri)){ // Add single row to cursor Integer requestId = Integer.parseInt(uri.getLastPathSegment()); if (null != db.get(requestId)) { DataRecord dr = db.get(requestId); cursor.addRow(new Object[] { dr.getID(), dr.getData() }); } } return cursor; } // Ignore request @Override public synchronized int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } // Initialize ContentProvider // Nothing to do in this case @Override public boolean onCreate() { return true; } // Does last segment of the Uri match a string of digits? private boolean isItemUri(Uri uri) { return uri.getLastPathSegment().matches("\\d+"); } // Is the last segment of the Uri the name of the data table? private boolean isTableUri(Uri uri) { return uri.getLastPathSegment().equals(DataContract.DATA_TABLE); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> |
list_layout.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="true" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/idString" android:layout_marginLeft="10dip"></TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/data" android:layout_marginLeft="10dip"></TextView> </LinearLayout> |