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
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.grocery_search"
android:resource="@xml/grocery_search" />
</activity>
</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package ly.generalassemb.drewmahrt.shoppinglistwithsearch;

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;
import android.support.v7.widget.SearchView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.support.v7.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private ListView mShoppingListView;
Expand All @@ -28,12 +29,35 @@ 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);

Intent intent = getIntent();
handleIntent(intent);
}

@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);
Toast.makeText(MainActivity.this, "Searching for " + query, Toast.LENGTH_SHORT).show();
Cursor cursor = ShoppingSQLiteOpenHelper.getInstance(this).searchGroceries(query);

Log.d("YOOYOYOOY", cursor.getCount() + "");
mCursorAdapter.swapCursor(cursor);
}

}

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);

return super.onCreateOptionsMenu(menu);
SearchManager manager = (SearchManager) getSystemService(SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,22 @@ 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
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)});
COL_ID + " = ?",
new String[]{String.valueOf(id)});
db.close();
return deleteNum;
}
Expand Down Expand Up @@ -140,4 +140,9 @@ private void loadShoppingInfo(SQLiteDatabase db) throws IOException {
reader.close();
}
}

public Cursor searchGroceries(String query) {
SQLiteDatabase db = getReadableDatabase();
return db.query(SHOPPING_LIST_TABLE_NAME, SHOPPING_COLUMNS, COL_ITEM_NAME + " LIKE '%" + query + "%' OR " + COL_ITEM_TYPE + " LIKE '%" + query + "%'", new String[]{query}, null, null, null, null);
}
}
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 groceries"/>