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.

35 changes: 35 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
@@ -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;
Expand All @@ -12,10 +14,13 @@
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private ListView mShoppingListView;
private CursorAdapter mCursorAdapter;
ShoppingSQLiteOpenHelper mHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -28,12 +33,42 @@ protected void onCreate(Bundle savedInstanceState) {

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

mHelper = ShoppingSQLiteOpenHelper.getInstance(MainActivity.this);

handleIntent(getIntent());
}

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

public void handleIntent(Intent intent){
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
// Do The actual database search

String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(MainActivity.this, "You are searched for " + query, Toast.LENGTH_SHORT).show();

Cursor newCursor = ShoppingSQLiteOpenHelper.getInstance(MainActivity.this).searchList(query);
mCursorAdapter.swapCursor(newCursor);

}
}

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

SearchableInfo info = searchManager.getSearchableInfo(getComponentName());

searchView.setSearchableInfo(info);

return super.onCreateOptionsMenu(menu);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Created by drewmahrt on 12/28/15.
*/
public class ShoppingSQLiteOpenHelper extends SQLiteOpenHelper{
public class ShoppingSQLiteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = ShoppingSQLiteOpenHelper.class.getCanonicalName();

private static final int DATABASE_VERSION = 7;
Expand All @@ -32,7 +32,7 @@ public class ShoppingSQLiteOpenHelper extends SQLiteOpenHelper{

private Context mHelperContext;

public static final String[] SHOPPING_COLUMNS = {COL_ID,COL_ITEM_NAME,COL_ITEM_DESCRIPTION,COL_ITEM_PRICE,COL_ITEM_TYPE};
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 +
Expand All @@ -46,8 +46,8 @@ public class ShoppingSQLiteOpenHelper extends SQLiteOpenHelper{

private static ShoppingSQLiteOpenHelper instance;

public static ShoppingSQLiteOpenHelper getInstance(Context context){
if(instance == null){
public static ShoppingSQLiteOpenHelper getInstance(Context context) {
if (instance == null) {
instance = new ShoppingSQLiteOpenHelper(context);
}
return instance;
Expand All @@ -61,10 +61,10 @@ private ShoppingSQLiteOpenHelper(Context context) {
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_SHOPPING_LIST_TABLE);
try{
try {
loadShoppingInfo(db);
}catch (Exception e){
Log.e(TAG,e.toString());
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}

Expand All @@ -75,7 +75,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

//Add new itinerary list
public long addItem(String name, String description, String price, String type){
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);
Expand All @@ -88,7 +88,7 @@ public long addItem(String name, String description, String price, String type){
return returnId;
}

public Cursor getShoppingList(){
public Cursor getShoppingList() {

SQLiteDatabase db = this.getReadableDatabase();

Expand All @@ -104,7 +104,7 @@ public Cursor getShoppingList(){
return cursor;
}

public int deleteItem(int id){
public int deleteItem(int id) {
SQLiteDatabase db = getWritableDatabase();
int deleteNum = db.delete(SHOPPING_LIST_TABLE_NAME,
COL_ID + " = ?",
Expand Down Expand Up @@ -132,12 +132,29 @@ private void loadShoppingInfo(SQLiteDatabase db) throws IOException {
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]);
} else {
Log.d(TAG, "Added item to database: " + strings[0]);
}
}
} finally {
reader.close();
}
}

public Cursor searchList(String query) {
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursorSearch = db.query(SHOPPING_LIST_TABLE_NAME, // a. table
SHOPPING_COLUMNS, // b. column names
COL_ITEM_NAME + " LIKE ?", // c. selections
new String[]{"%" + query + "%"}, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit

return cursorSearch;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
android:id="@+id/shopping_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>

<TextView
android:id="@+id/shopping_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/shopping_list_view"/>
</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemNameTextView"/>
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
</menu>

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?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 grocery item..."/>