· 6 years ago · Jun 14, 2019, 04:26 AM
1package app.android.com.todo.data;
2
3import android.net.Uri;
4import android.provider.BaseColumns;
5
6public final class TodoContract {
7//URI
8public static final String CONTENT_AUTHORITY="app.android.com.todo.todosprovider";
9
10public static final String PATH_CATEGORIES= "categories";
11public static final Uri BASE_CONTENT_URI= Uri.parse("content://"+ CONTENT_AUTHORITY);
12
13public static final class CategoriesEntry implements BaseColumns {
14 public static final Uri CONTENT_URI= Uri.withAppendedPath(BASE_CONTENT_URI,PATH_CATEGORIES);
15
16 // Table name
17 public static final String TABLE_NAME = "categories";
18 //column names
19 public static final String _ID = BaseColumns._ID;
20 public static final String COLUMN_DESCRIPTION = "description";
21}
22
23private void CreateCategory(){
24ContentValues values = new ContentValues(1);
25values.put(TodoContract.CategoriesEntry.
26 COLUMN_DESCRIPTION, "meme");
27
28Uri uri= getContentResolver().insert
29 (TodoContract.CategoriesEntry.CONTENT_URI, values);
30System.out.println( TodoContract.CategoriesEntry.CONTENT_URI+
31 " values "+ values + " inserted category "+ uri);
32
33}
34
35import android.content.ContentValues;
36import android.content.Context;
37import android.database.sqlite.SQLiteDatabase;
38import android.database.sqlite.SQLiteOpenHelper;
39
40import app.android.com.todo.data.TodoContract.CategoriesEntry;
41import app.android.com.todo.data.TodoContract.TodosEntry;
42
43public class DatabaseHelper extends SQLiteOpenHelper{
44
45private static final String DATABASE_NAME = "todosapp.db";
46private static final int DATABASE_VERSION = 1;
47private static final String TABLE_CATEGORIES_CREATE=
48 "CREATE TABLE " + CategoriesEntry.TABLE_NAME + " (" +
49 CategoriesEntry._ID + " INTEGER PRIMARY KEY, " +
50 CategoriesEntry.COLUMN_DESCRIPTION + " TEXT " +
51 ")";
52
53
54public DatabaseHelper(Context context ) {
55 super(context, DATABASE_NAME, null, DATABASE_VERSION);
56}
57
58@Override
59public void onCreate(SQLiteDatabase db) {
60
61 db.execSQL(TABLE_CATEGORIES_CREATE);
62
63
64}
65@Override
66public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
67
68 db.execSQL("DROP TABLE IF EXISTS " + CategoriesEntry.TABLE_NAME);
69 onCreate(db);
70}
71
72public class TodosProvider extends ContentProvider {
73//constants for the operation, the amount does not matter, must be unique
74
75private static final int TODOS=1;
76private static final int TODOS_ID=2;
77private static final int CATEGORIES=3;
78private static final int CATEGORIES_ID=4;
79
80private static final UriMatcher uriMatcher=
81 new UriMatcher(UriMatcher.NO_MATCH);
82
83static{
84
85
86 uriMatcher.addURI(CONTENT_AUTHORITY,
87 PATH_CATEGORIES,CATEGORIES);
88 uriMatcher.addURI(CONTENT_AUTHORITY,
89 PATH_CATEGORIES + "/#", CATEGORIES_ID);
90}
91
92private DatabaseHelper helper;
93
94@Override
95public boolean onCreate() {
96 helper= new DatabaseHelper(getContext());
97 return false;
98}