Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions starter-code/ShoppingListWithSearch/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions starter-code/ShoppingListWithSearch/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions starter-code/ShoppingListWithSearch/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions starter-code/ShoppingListWithSearch/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions starter-code/ShoppingListWithSearch/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions starter-code/ShoppingListWithSearch/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions starter-code/ShoppingListWithSearch/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
Expand All @@ -22,18 +23,41 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mShoppingListView = (ListView)findViewById(R.id.shopping_list_view);
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);
mCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, cursor, new String[]{ShoppingSQLiteOpenHelper.COL_ITEM_NAME, ShoppingSQLiteOpenHelper.COL_ITEM_TYPE}, new int[]{R.id.item_name, R.id.item_type}, 0);
mShoppingListView.setAdapter(mCursorAdapter);

handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}

private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())){
String query = intent.getStringExtra(SearchManager.QUERY);
Cursor cursor = ShoppingSQLiteOpenHelper.getInstance(MainActivity.this).searchFood(query);
mCursorAdapter.swapCursor(cursor);

}
}


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();

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ public Cursor getShoppingList(){

return cursor;
}
public Cursor searchFood(String query){

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(SHOPPING_LIST_TABLE_NAME, // a. table
SHOPPING_COLUMNS, // b. column names
COL_ITEM_NAME+" LIKE ? OR "+COL_ITEM_TYPE+" LIKE ? ", // c. selections
new String[]{"%" +query + "%", "%"+query+"%"}, // 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ITEM NAME"
android:textSize="30sp"/>

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>

<TextView
android:id="@+id/item_type"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TYPE"
android:textStyle="bold"
android:textSize="30sp"/>


</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search for something...">
</searchable>