From 3a37014fcd2fc6418864f27442bbea1ab02d74bd Mon Sep 17 00:00:00 2001 From: WasabiMayo Date: Wed, 3 Feb 2016 13:05:16 -0500 Subject: [PATCH] Claire & Will - Search View Lab --- .../ShoppingListWithSearch/.idea/.name | 1 + .../ShoppingListWithSearch/.idea/compiler.xml | 22 +++++++ .../.idea/copyright/profiles_settings.xml | 3 + .../.idea/encodings.xml | 6 ++ .../ShoppingListWithSearch/.idea/gradle.xml | 18 ++++++ .../ShoppingListWithSearch/.idea/misc.xml | 62 +++++++++++++++++++ .../ShoppingListWithSearch/.idea/modules.xml | 9 +++ .../.idea/runConfigurations.xml | 12 ++++ .../ShoppingListWithSearch/.idea/vcs.xml | 6 ++ .../app/src/main/AndroidManifest.xml | 4 ++ .../shoppinglistwithsearch/MainActivity.java | 30 ++++++++- .../ShoppingSQLiteOpenHelper.java | 12 ++++ .../main/res/layout/shopping_list_item.xml | 19 ++++++ .../app/src/main/res/xml/searchable.xml | 4 ++ 14 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 starter-code/ShoppingListWithSearch/.idea/.name create mode 100644 starter-code/ShoppingListWithSearch/.idea/compiler.xml create mode 100644 starter-code/ShoppingListWithSearch/.idea/copyright/profiles_settings.xml create mode 100644 starter-code/ShoppingListWithSearch/.idea/encodings.xml create mode 100644 starter-code/ShoppingListWithSearch/.idea/gradle.xml create mode 100644 starter-code/ShoppingListWithSearch/.idea/misc.xml create mode 100644 starter-code/ShoppingListWithSearch/.idea/modules.xml create mode 100644 starter-code/ShoppingListWithSearch/.idea/runConfigurations.xml create mode 100644 starter-code/ShoppingListWithSearch/.idea/vcs.xml create mode 100644 starter-code/ShoppingListWithSearch/app/src/main/res/layout/shopping_list_item.xml create mode 100644 starter-code/ShoppingListWithSearch/app/src/main/res/xml/searchable.xml diff --git a/starter-code/ShoppingListWithSearch/.idea/.name b/starter-code/ShoppingListWithSearch/.idea/.name new file mode 100644 index 0000000..10ee423 --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/.name @@ -0,0 +1 @@ +ShoppingListWithSearch \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/.idea/compiler.xml b/starter-code/ShoppingListWithSearch/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/.idea/copyright/profiles_settings.xml b/starter-code/ShoppingListWithSearch/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/.idea/encodings.xml b/starter-code/ShoppingListWithSearch/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/.idea/gradle.xml b/starter-code/ShoppingListWithSearch/.idea/gradle.xml new file mode 100644 index 0000000..8d2df47 --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/.idea/misc.xml b/starter-code/ShoppingListWithSearch/.idea/misc.xml new file mode 100644 index 0000000..cca2cda --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/.idea/modules.xml b/starter-code/ShoppingListWithSearch/.idea/modules.xml new file mode 100644 index 0000000..27a232c --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/.idea/runConfigurations.xml b/starter-code/ShoppingListWithSearch/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/.idea/vcs.xml b/starter-code/ShoppingListWithSearch/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/starter-code/ShoppingListWithSearch/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/app/src/main/AndroidManifest.xml b/starter-code/ShoppingListWithSearch/app/src/main/AndroidManifest.xml index 77155bc..9ac2db0 100644 --- a/starter-code/ShoppingListWithSearch/app/src/main/AndroidManifest.xml +++ b/starter-code/ShoppingListWithSearch/app/src/main/AndroidManifest.xml @@ -13,7 +13,11 @@ + + + diff --git a/starter-code/ShoppingListWithSearch/app/src/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/MainActivity.java b/starter-code/ShoppingListWithSearch/app/src/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/MainActivity.java index ab2c048..90a1d97 100644 --- a/starter-code/ShoppingListWithSearch/app/src/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/MainActivity.java +++ b/starter-code/ShoppingListWithSearch/app/src/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/MainActivity.java @@ -1,7 +1,9 @@ package ly.generalassemb.drewmahrt.shoppinglistwithsearch; import android.app.SearchManager; +import android.app.SearchableInfo; import android.content.Context; +import android.content.Intent; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; @@ -12,6 +14,7 @@ import android.widget.CursorAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; +import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ListView mShoppingListView; @@ -26,14 +29,37 @@ protected void onCreate(Bundle savedInstanceState) { 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); + mCursorAdapter = new SimpleCursorAdapter(this,R.layout.shopping_list_item,cursor,new String[]{ShoppingSQLiteOpenHelper.COL_ITEM_NAME,ShoppingSQLiteOpenHelper.COL_ITEM_TYPE},new int[]{R.id.item_name_textview, R.id.item_type_textview},0); mShoppingListView.setAdapter(mCursorAdapter); + + handleIntent(getIntent()); + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + handleIntent(intent); + } + + private void handleIntent(Intent intent){ + if(Intent.ACTION_SEARCH.equals(intent.getAction())){ + String query = intent.getStringExtra(SearchManager.QUERY); + Toast.makeText(MainActivity.this, "Working. Received query : "+query, Toast.LENGTH_SHORT).show(); + Cursor newCursor = ShoppingSQLiteOpenHelper.getInstance(MainActivity.this).searchQuery(query); + mCursorAdapter.swapCursor(newCursor); + } } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.main_menu, menu); - return super.onCreateOptionsMenu(menu); + SearchManager searchManager = (SearchManager)getSystemService(SEARCH_SERVICE); + SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); + + SearchableInfo info = searchManager.getSearchableInfo( getComponentName() ); + searchView.setSearchableInfo(info); + + return true; } } diff --git a/starter-code/ShoppingListWithSearch/app/src/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/ShoppingSQLiteOpenHelper.java b/starter-code/ShoppingListWithSearch/app/src/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/ShoppingSQLiteOpenHelper.java index f5a5974..f96e80c 100644 --- a/starter-code/ShoppingListWithSearch/app/src/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/ShoppingSQLiteOpenHelper.java +++ b/starter-code/ShoppingListWithSearch/app/src/main/java/ly/generalassemb/drewmahrt/shoppinglistwithsearch/ShoppingSQLiteOpenHelper.java @@ -140,4 +140,16 @@ private void loadShoppingInfo(SQLiteDatabase db) throws IOException { reader.close(); } } + + public Cursor searchQuery(String query){ + SQLiteDatabase db = this.getReadableDatabase(); + + String selection = COL_ITEM_NAME+" LIKE ? OR " + COL_ITEM_TYPE + " LIKE ?"; + String[] selectionArgs = new String[]{ "%"+query+"%", "%"+query+"%" }; + Cursor cursor = db.query(SHOPPING_LIST_TABLE_NAME, SHOPPING_COLUMNS, selection, selectionArgs, null, null, null, null); + + return cursor; + + } + } diff --git a/starter-code/ShoppingListWithSearch/app/src/main/res/layout/shopping_list_item.xml b/starter-code/ShoppingListWithSearch/app/src/main/res/layout/shopping_list_item.xml new file mode 100644 index 0000000..ef2de28 --- /dev/null +++ b/starter-code/ShoppingListWithSearch/app/src/main/res/layout/shopping_list_item.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/starter-code/ShoppingListWithSearch/app/src/main/res/xml/searchable.xml b/starter-code/ShoppingListWithSearch/app/src/main/res/xml/searchable.xml new file mode 100644 index 0000000..c74281b --- /dev/null +++ b/starter-code/ShoppingListWithSearch/app/src/main/res/xml/searchable.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file