diff --git a/main/AndroidManifest.xml b/main/AndroidManifest.xml new file mode 100644 index 0000000..5a194f5 --- /dev/null +++ b/main/AndroidManifest.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + diff --git a/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/MainActivity.java b/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/MainActivity.java new file mode 100644 index 0000000..de7ed37 --- /dev/null +++ b/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/MainActivity.java @@ -0,0 +1,58 @@ +package ly.generalassemb.drewmahrt.shoppinglistwithsearch; + +import android.app.SearchManager; +import android.content.Intent; +import android.database.Cursor; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.SearchView; +import android.view.Menu; +import android.view.MenuInflater; +import android.widget.CursorAdapter; +import android.widget.ListView; +import android.widget.SimpleCursorAdapter; + +public class MainActivity extends AppCompatActivity { + private ListView mShoppingListView; + private CursorAdapter mCursorAdapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + mShoppingListView = (ListView)findViewById(R.id.shopping_list_view); + + Cursor cursor = ShoppingSQLiteOpenHelper.getInstance(MainActivity.this).getShoppingList(); + + mCursorAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,new String[]{ShoppingSQLiteOpenHelper.COL_ITEM_NAME},new int[]{android.R.id.text1},0); + mShoppingListView.setAdapter(mCursorAdapter); + } + + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater menuInflater = getMenuInflater(); + menuInflater.inflate(R.menu.main_menu, menu); + SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); + SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); + searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); + return true; + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + handleIntent(intent); + } + + private void handleIntent(Intent intent) { + if (Intent.ACTION_SEARCH.equals( intent.getAction() )) { +// ShoppingSQLiteOpenHelper helper = ShoppingSQLiteOpenHelper.getInstance(this); +// Cursor cursor = helper.getShoppingList(); + Cursor cursor = ShoppingSQLiteOpenHelper.getInstance(MainActivity.this).getShoppingList(); + if (cursor.getString(cursor.getColumnIndex("ITEM_NAME")).contains(intent.getStringExtra(SearchManager.QUERY).{ + + } + + } + } +} diff --git a/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/ShoppingSQLiteOpenHelper.java b/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/ShoppingSQLiteOpenHelper.java new file mode 100644 index 0000000..cbfb353 --- /dev/null +++ b/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/ShoppingSQLiteOpenHelper.java @@ -0,0 +1,148 @@ +package ly.generalassemb.drewmahrt.shoppinglistwithsearch; + +import android.content.ContentValues; +import android.content.Context; +import android.content.res.Resources; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.text.TextUtils; +import android.util.Log; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * Created by drewmahrt on 12/28/15. + */ +public class ShoppingSQLiteOpenHelper extends SQLiteOpenHelper{ + private static final String TAG = ShoppingSQLiteOpenHelper.class.getCanonicalName(); + + private static final int DATABASE_VERSION = 7; + public static final String DATABASE_NAME = "SHOPPING_DB"; + public static final String SHOPPING_LIST_TABLE_NAME = "SHOPPING_LIST"; + + public static final String COL_ID = "_id"; + public static final String COL_ITEM_NAME = "ITEM_NAME"; + public static final String COL_ITEM_PRICE = "PRICE"; + public static final String COL_ITEM_DESCRIPTION = "DESCRIPTION"; + public static final String COL_ITEM_TYPE = "TYPE"; + + private Context mHelperContext; + + public static final String[] SHOPPING_COLUMNS = {COL_ID,COL_ITEM_NAME,COL_ITEM_DESCRIPTION,COL_ITEM_PRICE,COL_ITEM_TYPE}; + + private static final String CREATE_SHOPPING_LIST_TABLE = + "CREATE TABLE " + SHOPPING_LIST_TABLE_NAME + + "(" + + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + + COL_ITEM_NAME + " TEXT, " + + COL_ITEM_DESCRIPTION + " TEXT, " + + COL_ITEM_PRICE + " TEXT, " + + COL_ITEM_TYPE + " TEXT )"; + + + private static ShoppingSQLiteOpenHelper instance; + + public static ShoppingSQLiteOpenHelper getInstance(Context context){ + if(instance == null){ + instance = new ShoppingSQLiteOpenHelper(context); + } + return instance; + } + + private ShoppingSQLiteOpenHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + mHelperContext = context; + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL(CREATE_SHOPPING_LIST_TABLE); + try{ + loadShoppingInfo(db); + }catch (Exception e){ + Log.e(TAG,e.toString()); + } + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL("DROP TABLE IF EXISTS " + SHOPPING_LIST_TABLE_NAME); + this.onCreate(db); + } + + //Add new itinerary list + public long addItem(String name, String description, String price, String type){ + ContentValues values = new ContentValues(); + values.put(COL_ITEM_NAME, name); + values.put(COL_ITEM_DESCRIPTION, description); + values.put(COL_ITEM_PRICE, price); + values.put(COL_ITEM_TYPE, type); + + SQLiteDatabase db = this.getWritableDatabase(); + long returnId = db.insert(SHOPPING_LIST_TABLE_NAME, null, values); + db.close(); + return returnId; + } + + public Cursor getShoppingList(){ + + SQLiteDatabase db = this.getReadableDatabase(); + + Cursor cursor = db.query(SHOPPING_LIST_TABLE_NAME, // a. table + SHOPPING_COLUMNS, // b. column names + null, // c. selections + null, // d. selections args + null, // e. group by + null, // f. having + null, // g. order by + null); // h. limit + + return cursor; + } + + public int deleteItem(int id){ + SQLiteDatabase db = getWritableDatabase(); + int deleteNum = db.delete(SHOPPING_LIST_TABLE_NAME, + COL_ID + " = ?", + new String[]{String.valueOf(id)}); + db.close(); + return deleteNum; + } + +// public String searchInput(){ +// SQLiteDatabase db = getReadableDatabase(); +// db.execSQL("SELECT" + COL_ITEM_NAME + "FROM" + SHOPPING_LIST_TABLE_NAME + "WHERE" + COL_ITEM_NAME "LIKE '"); +// } + + private void loadShoppingInfo(SQLiteDatabase db) throws IOException { + final Resources resources = mHelperContext.getResources(); + InputStream inputStream = resources.openRawResource(R.raw.prepopulated_shopping_list); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + + try { + String line; + ContentValues values; + while ((line = reader.readLine()) != null) { + String[] strings = TextUtils.split(line, "~"); + if (strings.length < 4) continue; + values = new ContentValues(); + values.put(COL_ITEM_NAME, strings[0].trim()); + values.put(COL_ITEM_DESCRIPTION, strings[1].trim()); + values.put(COL_ITEM_PRICE, strings[2].trim()); + values.put(COL_ITEM_TYPE, strings[3].trim()); + long id = db.insert(SHOPPING_LIST_TABLE_NAME, null, values); + if (id < 0) { + Log.e(TAG, "unable to add entry"); + }else{ + Log.d(TAG,"Added item to database: "+strings[0]); + } + } + } finally { + reader.close(); + } + } +} diff --git a/main/res/layout/activity_main.xml b/main/res/layout/activity_main.xml new file mode 100644 index 0000000..20b1774 --- /dev/null +++ b/main/res/layout/activity_main.xml @@ -0,0 +1,16 @@ + + + + + diff --git a/main/res/menu/main_menu.xml b/main/res/menu/main_menu.xml new file mode 100644 index 0000000..8c7082d --- /dev/null +++ b/main/res/menu/main_menu.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/main/res/mipmap-hdpi/ic_launcher.png b/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..cde69bc Binary files /dev/null and b/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/main/res/mipmap-mdpi/ic_launcher.png b/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..c133a0c Binary files /dev/null and b/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/main/res/mipmap-xhdpi/ic_launcher.png b/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..bfa42f0 Binary files /dev/null and b/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/main/res/mipmap-xxhdpi/ic_launcher.png b/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..324e72c Binary files /dev/null and b/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/main/res/mipmap-xxxhdpi/ic_launcher.png b/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..aee44e1 Binary files /dev/null and b/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/main/res/raw/prepopulated_shopping_list.txt b/main/res/raw/prepopulated_shopping_list.txt new file mode 100644 index 0000000..5d12df7 --- /dev/null +++ b/main/res/raw/prepopulated_shopping_list.txt @@ -0,0 +1,7 @@ +Bread~Whole Wheat Bread~2.35~Food +Milk~1 Gallon Skim Milk~3.50~Dairy +Ice Cream~Mint Chocolate Chip~2.20~Dairy +Paper Plates~White Paper Plates~7.50~Dishes +Chicken Breasts~Boneless Skinless~2.30~Poultry +Carrots~Baby Carrots~4.00~Produce +Lettuce~Iceberg~3.14~Produce diff --git a/main/res/values-w820dp/dimens.xml b/main/res/values-w820dp/dimens.xml new file mode 100644 index 0000000..63fc816 --- /dev/null +++ b/main/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ + + + 64dp + diff --git a/main/res/values/colors.xml b/main/res/values/colors.xml new file mode 100644 index 0000000..3ab3e9c --- /dev/null +++ b/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #3F51B5 + #303F9F + #FF4081 + diff --git a/main/res/values/dimens.xml b/main/res/values/dimens.xml new file mode 100644 index 0000000..47c8224 --- /dev/null +++ b/main/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 16dp + 16dp + diff --git a/main/res/values/strings.xml b/main/res/values/strings.xml new file mode 100644 index 0000000..74b8910 --- /dev/null +++ b/main/res/values/strings.xml @@ -0,0 +1,4 @@ + + ShoppingListWithSearch + Search for a grocery item + diff --git a/main/res/values/styles.xml b/main/res/values/styles.xml new file mode 100644 index 0000000..5885930 --- /dev/null +++ b/main/res/values/styles.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/main/res/xml/searchable.xml b/main/res/xml/searchable.xml new file mode 100644 index 0000000..8b99a0a --- /dev/null +++ b/main/res/xml/searchable.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file