· 8 years ago · Jun 13, 2018, 06:18 AM
1package com.example.android.inventory.DATA;
2
3import android.content.Context;
4import android.database.sqlite.SQLiteDatabase;
5import android.database.sqlite.SQLiteOpenHelper;
6
7import com.example.android.inventory.DATA.InventoryContract.InventoryEntry;
8import com.example.android.inventory.DATA.InventoryContract.SupplierEntry;
9
10public class InventoryDbHelper extends SQLiteOpenHelper {
11public static final String DATABASE_NAME = "INVENTORY";
12public static final int DATABSE_VERSION = 1;
13public static final String CRETE_INVENTORY_TABLE =
14 "CREATE TABLE "+ InventoryEntry.INVENTORY_TABLE_NAME+
15 "("+
16 InventoryEntry.INVENTORY_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"+
17 InventoryEntry.INVENTORY_COLUMN_NAME + " TEXT NOT NULL,"+
18 InventoryEntry.INVENTORY_COLUMN_PRICE + " INTEGER NOT NULL,"+
19 InventoryEntry.INVENTORY_COLUMN_QUANTITY+" INTEGER NOT NULL DEFAULT 0,"+
20 InventoryEntry.INVENTORY_COLUMN_SUPPLIER_NAME + " TEXT);";
21
22public static final String DELETEINVENTORY_TABLE = "DROP TABLE IF EXISTS "+InventoryEntry.INVENTORY_TABLE_NAME+";";
23
24public static final String CREATE_SUPPLIER_TABLE =
25 "CREATE TABLE "+ SupplierEntry.SUPPLIER_TABLE_NAME+
26 "("+
27 SupplierEntry.SUPPLIER_COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+
28 SupplierEntry.SUPPLIER_COLUMN_NAME+" TEXT NOT NULL,"+
29 SupplierEntry.SUPPLIER_COLUMN_EMAIL + " TEXT,"+
30 SupplierEntry.SUPPLIER_COLUMN_PHONE+" INTEGER"+
31 ");";
32
33public static final String DELETE_SUPPLIER_TABLE = "DROP TABLE IF EXISTS "+SupplierEntry.SUPPLIER_TABLE_NAME+";";
34
35public InventoryDbHelper(Context context){
36 super(context,DATABASE_NAME,null,DATABSE_VERSION);
37}
38
39
40@Override
41public void onCreate(SQLiteDatabase db) {
42 db.execSQL(CRETE_INVENTORY_TABLE);
43 db.execSQL(CREATE_SUPPLIER_TABLE);
44}
45
46@Override
47public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
48 db.execSQL(DELETE_SUPPLIER_TABLE);
49 db.execSQL(DELETEINVENTORY_TABLE);
50 onCreate(db);
51}
52
53package com.example.android.inventory;
54
55import android.app.LoaderManager;
56import android.content.CursorLoader;
57import android.content.Intent;
58import android.content.Loader;
59import android.database.Cursor;
60import android.support.v7.app.AppCompatActivity;
61import android.os.Bundle;
62import android.util.Log;
63import android.view.Menu;
64import android.view.MenuItem;
65import android.widget.ImageView;
66import android.widget.ListView;
67
68import com.example.android.inventory.DATA.InventoryAdapter;
69import com.example.android.inventory.DATA.InventoryContract;
70
71public class MainActivity extends AppCompatActivity implements
72LoaderManager.LoaderCallbacks<Cursor> {
73ListView listView;
74InventoryAdapter inventoryAdapter = new InventoryAdapter(this,null);
75
76@Override
77protected void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
79 setContentView(R.layout.activity_main);
80 listView = (ListView)findViewById(R.id.list_view);
81 listView.setAdapter(inventoryAdapter);
82 listView.setEmptyView(findViewById(R.id.imageView));
83 getLoaderManager().initLoader(0,null,this);
84}
85
86@Override
87public boolean onCreateOptionsMenu(Menu menu) {
88 getMenuInflater().inflate(R.menu.first_page_menu,menu);
89 return true;
90}
91
92@Override
93public boolean onOptionsItemSelected(MenuItem item) {
94 int item_id = item.getItemId();
95 switch (item_id){
96 case R.id.insert_inventory:
97 InsertInventory();
98 return true;
99 default:
100 return super.onOptionsItemSelected(item);
101 }
102
103}
104
105private void InsertInventory() {
106 Intent intent = new Intent(this,InsertInventory.class);
107 startActivity(intent);
108}
109
110@Override
111public Loader<Cursor> onCreateLoader(int id, Bundle args) {
112 return new CursorLoader(this, InventoryContract.InventoryEntry.INVENTORY_TABLE_BASE_URI,null,null,null,null);
113}
114
115@Override
116public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
117 inventoryAdapter.swapCursor(data);
118 Log.e("done","done2");
119}
120
121@Override
122public void onLoaderReset(Loader<Cursor> loader) {
123 inventoryAdapter.swapCursor(null);
124}
125
126package com.example.android.inventory.DATA;
127
128import android.content.ContentProvider;
129import android.content.ContentUris;
130import android.content.ContentValues;
131import android.content.UriMatcher;
132import android.database.Cursor;
133import android.database.sqlite.SQLiteDatabase;
134import android.net.Uri;
135import android.support.annotation.NonNull;
136import android.support.annotation.Nullable;
137import android.util.Log;
138
139import com.example.android.inventory.DATA.InventoryContract.InventoryEntry;
140
141public class InventoryProvider extends ContentProvider {
142InventoryDbHelper helper;
143public static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
144public static final int URI_INVENTORY_TABLE = 1;
145public static final int URI_INVENTORY_ROW = 2;
146public static final int URI_SUPPLIER_TABLE = 3;
147public static final int URI_SIPPLIER_ROW = 4;
148
149static {
150 uriMatcher.addURI(InventoryContract.AUTHORITY, InventoryEntry.INVENTORY_TABLE_NAME,URI_INVENTORY_TABLE);
151 uriMatcher.addURI(InventoryContract.AUTHORITY, InventoryEntry.INVENTORY_TABLE_NAME+"/#",URI_INVENTORY_ROW);
152
153 uriMatcher.addURI(InventoryContract.AUTHORITY,InventoryContract.SupplierEntry.SUPPLIER_TABLE_NAME,URI_SUPPLIER_TABLE);
154 uriMatcher.addURI(InventoryContract.AUTHORITY, InventoryEntry.INVENTORY_TABLE_NAME+"/#",URI_SIPPLIER_ROW);
155
156}
157@Override
158public boolean onCreate() {
159 helper = new InventoryDbHelper(getContext());
160 return true;
161}
162
163@Nullable
164@Override
165public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
166 Cursor cursor = null;
167 SQLiteDatabase db = helper.getReadableDatabase();
168 projection = new String[]{InventoryEntry.INVENTORY_COLUMN_NAME, InventoryEntry.INVENTORY_COLUMN_PRICE};
169 int match = uriMatcher.match(uri);
170 switch (match) {
171 case URI_INVENTORY_TABLE:
172 cursor = db.query(InventoryEntry.INVENTORY_TABLE_NAME,projection,null,null,null,null,null);
173 break;
174 case URI_INVENTORY_ROW:
175 selection = InventoryEntry.INVENTORY_COLUMN_ID +"=?";
176 selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
177 cursor = db.query(InventoryEntry.INVENTORY_TABLE_NAME,projection,selection,selectionArgs,null,null,null);
178 break;
179 default:
180 cursor=null;
181 }
182 cursor.setNotificationUri(getContext().getContentResolver(),uri);
183 Log.e("done","done");
184 return cursor;
185}
186
187@Nullable
188@Override
189public String getType(@NonNull Uri uri) {
190 return null;
191}
192
193@Nullable
194@Override
195public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
196 long ml;
197 SQLiteDatabase db = helper.getReadableDatabase();
198 int match = uriMatcher.match(uri);
199 switch (match) {
200 case URI_INVENTORY_TABLE:
201 ml = db.insert(InventoryEntry.INVENTORY_TABLE_NAME,null,values);
202 break;
203 default:
204 throw new IllegalArgumentException("wrong uri");
205 }
206 if (ml == -1) {
207 throw new IllegalArgumentException("error with insertion");
208 } else {
209 return Uri.withAppendedPath(InventoryEntry.INVENTORY_TABLE_BASE_URI, String.valueOf(ml));
210 }
211
212}
213
214@Override
215public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
216 return 0;
217}
218
219@Override
220public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
221 return 0;
222}