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
2 changes: 1 addition & 1 deletion starter-code/ShoppingListVer2/.idea/compiler.xml

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

5 changes: 3 additions & 2 deletions starter-code/ShoppingListVer2/.idea/gradle.xml

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

18 changes: 17 additions & 1 deletion starter-code/ShoppingListVer2/.idea/misc.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
@@ -0,0 +1,20 @@
package ly.generalassemb.drewmahrt.shoppinglistver2;

/**
* Created by sbabba on 2/2/16.
*/
public class GroceryList {
int _id;
String item_name;
String description;
String price;
String type;

public GroceryList(int _id, String item_name, String description, String price, String type) {
this._id = _id;
this.item_name = item_name;
this.description = description;
this.price = price;
this.type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ly.generalassemb.drewmahrt.shoppinglistver2;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;

/**
* Created by sbabba on 2/2/16.
*/
public class GroceryListHelper extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 7;
public static final String DATABASE_NAME = "SHOPPING_DB";
public static final String SHOPPING_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_DESCRIPTION = "DESCRIPTION";
public static final String COL_PRICE = "PRICE";
public static final String COL_TYPE = "TYPE";

public static final String[] GROCERY_COLUMNS = {COL_ID,COL_ITEM_NAME,COL_DESCRIPTION,COL_PRICE,COL_TYPE};

public static final String CREATE_GROCERY_LIST_TABLE =
"CREATE TABLE" + SHOPPING_TABLE_NAME +
"(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_ITEM_NAME + " TEXT, " +
COL_DESCRIPTION + " TEXT, " +
COL_PRICE + " TEXT, " +
COL_TYPE + " TEXT )";


public GroceryListHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_GROCERY_LIST_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + SHOPPING_TABLE_NAME);
this.onCreate(db);
}

public Cursor getGroceryList(){

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(SHOPPING_TABLE_NAME, // a. table
GROCERY_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 void removeItems(String name) {
String selection = "ITEM_NAME = ?";

String[] selectionArgs = {name};

SQLiteDatabase db = this.getWritableDatabase();
db.delete("SHOPPING_LIST", selection,selectionArgs);

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

import ly.generalassemb.drewmahrt.shoppinglistver2.setup.DBAssetHelper;

public class MainActivity extends AppCompatActivity {
GroceryListHelper mHelper;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -22,5 +32,53 @@ protected void onCreate(Bundle savedInstanceState) {
DBAssetHelper dbSetup = new DBAssetHelper(MainActivity.this);
dbSetup.getReadableDatabase();

mHelper = new GroceryListHelper(MainActivity.this);
Cursor cursor = mHelper.getGroceryList();

final CursorAdapter cursorAdapter = new CursorAdapter(MainActivity.this, cursor, 0) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item_layout, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

TextView nameTextView = (TextView) view.findViewById(R.id.name_text_view);
TextView descriptionTextView = (TextView) view.findViewById(R.id.description_text_view);
TextView priceTextView = (TextView) view.findViewById(R.id.price_text_view);
TextView typeTextView = (TextView) view.findViewById(R.id.type_text_view);


String item_name = cursor.getString(cursor.getColumnIndex(GroceryListHelper.COL_ITEM_NAME));
String description = cursor.getString(cursor.getColumnIndex(GroceryListHelper.COL_DESCRIPTION));
String price = cursor.getString(cursor.getColumnIndex(GroceryListHelper.COL_PRICE));
String type = cursor.getString(cursor.getColumnIndex(GroceryListHelper.COL_TYPE));

nameTextView.setText(item_name);
descriptionTextView.setText(description);
priceTextView.setText(price);
typeTextView.setText(type);

}
};

ListView listView = (ListView) findViewById(R.id.shopping_list_view);
listView.setAdapter(cursorAdapter);


AdapterView.OnItemLongClickListener longClickListener = new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
String itemNameRemove = ((TextView)view.findViewById(R.id.name_text_view)).getText().toString();

mHelper.removeItems(itemNameRemove);
Cursor cursorInside = mHelper.getGroceryList();
cursorAdapter.swapCursor(cursorInside);
return true;
}
};
listView.setOnItemLongClickListener(longClickListener);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ly.generalassemb.drewmahrt.shoppinglistver2.setup;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

Expand All @@ -15,4 +17,7 @@ public class DBAssetHelper extends SQLiteAssetHelper {
public DBAssetHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}

};


Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/description_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/type_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>