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
24 changes: 24 additions & 0 deletions main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ly.generalassemb.drewmahrt.shoppinglistwithsearch">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<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>

</manifest>
Original file line number Diff line number Diff line change
@@ -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).{

}

}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
16 changes: 16 additions & 0 deletions main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ly.generalassemb.drewmahrt.shoppinglistwithsearch.MainActivity">

<ListView
android:id="@+id/shopping_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
10 changes: 10 additions & 0 deletions main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Binary file added main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions main/res/raw/prepopulated_shopping_list.txt
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
6 changes: 6 additions & 0 deletions main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
5 changes: 5 additions & 0 deletions main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
4 changes: 4 additions & 0 deletions main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">ShoppingListWithSearch</string>
<string name="search_hint">Search for a grocery item</string>
</resources>
11 changes: 11 additions & 0 deletions main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>
4 changes: 4 additions & 0 deletions main/res/xml/searchable.xml
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 a grocery item..."/>