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
6 changes: 6 additions & 0 deletions starter-code/ShoppingListVer2/.idea/encodings.xml

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

3 changes: 2 additions & 1 deletion starter-code/ShoppingListVer2/.idea/gradle.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 @@ -11,6 +11,7 @@
import android.widget.SimpleCursorAdapter;

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

public class MainActivity extends AppCompatActivity {
@Override
Expand All @@ -22,5 +23,26 @@ protected void onCreate(Bundle savedInstanceState) {
DBAssetHelper dbSetup = new DBAssetHelper(MainActivity.this);
dbSetup.getReadableDatabase();

//Link SQL Helper
//Creating the helper

//Instantiate ShoppingSQLiteHelper object
ShoppingSQLiteHelper shoppingHelper = new ShoppingSQLiteHelper(MainActivity.this);

//get access to that method in the Class
//*must have matching datatypes "Cursor"
Cursor cursor = shoppingHelper.cursorOfShoppingList();

//Create Array "ITEM_NAME" column for Cursor Adapter below
String[] columnName = new String[]{"ITEM_NAME"};
//create Integer layoutIds to store View ID of simple_list_item_1 below
int[] layoutIds = new int[]{android.R.id.text1};

//BUILD CURSOR ADAPTER
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(MainActivity.this, android.R.layout.simple_list_item_1, cursor, columnName, layoutIds, 0);

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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ly.generalassemb.drewmahrt.shoppinglistver2.setup;

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

/**
* Created by Envy on 2/2/2016.
*/
public class ShoppingSQLiteHelper extends SQLiteOpenHelper {

//VERSION is private
//Specify title names of Table
private static final int DATABASE_VERSION = 7;
public static final String DATABASE_NAME = "SHOPPING_DB";
public static final String TABLE_NAME = "SHOPPING_LIST";

public static final String ID = "_id";
public static final String ITEM_NAME = "ITEM_NAME";
public static final String DESCRIPTION = "DESCRIPTION";
public static final String PRICE = "PRICE";
public static final String TYPE = "TYPE";

public static final String[] COLUMN_NAMES = new String[]{ ID, ITEM_NAME, DESCRIPTION, PRICE, TYPE };

//Code Generator > CONSTRUCTOR:
//Delete String Name = Variable declared already
public ShoppingSQLiteHelper(Context context) {
//create null, and link DATABASE_NAME, DATABASE_VERSION
//removed parameter items that wont be used
//super is a constructor for parent class
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

//CREATING DATABASE: CODE GENERATOR > ONCREATE > ONUPGRADE
//ONCREATE, TO CREATE DATABASE
@Override
public void onCreate(SQLiteDatabase db) {
//DATABASE CREATION
//execSQL = Takes a string as a parameter
//db.exeSQL( "CREATE TABLE table_name ( column names DATATYPES)")
db.execSQL("CREATE TABLE SHOPPING_LIST ( _id INTEGER PRIMARY KEY AUTO ICREMENT, ITEM_NAME TEXT, DESCRIPTION TEXT, PRICE TEXT, TYPE TEXT )");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//onUpgrade = Database Manipulation to recreate a new database

//Erase, "drop" old SHOPPING_LIST if exists
//drop "SHOPPING_LIST"
db.execSQL("DROP TABLE IF EXISTS SHOPPING_LIST");

//Then create new 'db' below
this.onCreate(db);
}

//Refer to table where content is located
//Create method to RETURN a Cursor

public Cursor cursorOfShoppingList(){

//Specify which database Cursor will be used
//.getReadableDatabase will automatically grab database
SQLiteDatabase databaze = this.getReadableDatabase();

//Refer the Cursor to new databaze
//SELECT * FROM SHOPPING_LIST WHERE ITEM_NAME = "Milk";
Cursor cursor = databaze.query(TABLE_NAME, COLUMN_NAMES,null,null,null,null,null,null);

return cursor;
}




}