0

Hi guys I'm trying to develop a project that will get the database items to a gridview so I'm trying several things. Now what I ask is how to populate my gridview using this codes of mine BTW im using sqlite

This is my databasehelper

// Database Name
private static final String DATABASE_NAME = "SMART_CART";

// Item table name
private static final String TBL_ITEMS = "tblItems";

// Item Table Columns names
private static final String ITEM_BARCODE = "Barcode_ID";
private static final String ITEM_NAME = "Item_Name";
private static final String ITEM_PRICE = "Item_Price";
private static final String ITEM_CATEGORY = "Item_Category";
private static final String ITEM_IMG = "Item_IMG";

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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TBL_ITEMS + "("
            + ITEM_BARCODE + " INTEGER PRIMARY KEY," + ITEM_NAME + " NVARCHAR(255),"
            + ITEM_PRICE + " REAL," + ITEM_CATEGORY + " TEXT," 
            + ITEM_IMG + "BLOB" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TBL_ITEMS);

    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new item
void addItems(Items items) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(ITEM_BARCODE, items.getBarcode()); // Barcode
    values.put(ITEM_NAME, items.getName()); // Item name
    values.put(ITEM_PRICE, items.getPrice()); // Item Price
    values.put(ITEM_CATEGORY, items.getCategory()); // Item Category
    values.put(ITEM_IMG, items.getImage()); // Item Image
    // Inserting Row
    db.insert(TBL_ITEMS, null, values);
    db.close(); // Closing database connection
}`

And this is my Items class

public class Items {


public Items() {
}
    public Items(int barcode, String name, float price, String category,
            String image) {
        super();
        this.barcode = barcode;
        this.name = name;
        this.price = price;
        this.category = category;
        this.image = image;
}



int barcode;
String name;
float price;
String category;
String image;


public int getBarcode() {
return barcode;
}
public void setBarcode(int barcode) {
this.barcode = barcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}

public String getImage() {
    return image;
}
public void setImage(String image) {
this.image = image;
}
}

Now my question is how can i populate my gridview with all those data i have in my database and does my process of storing an image in the database is correct? I already have images in one of the drawable folder.

Any help would be much appreciated thank you in advance

HelloWorld
  • 17
  • 1
  • 9
  • there are lot of examples on it..once google it?? – kalyan pvs Dec 14 '13 at 03:25
  • @kalyanpvs Yes i've searched about it but i cant seem to find an answer. Could you please make an example? I'm really new to android i'm trying to learn. – HelloWorld Dec 14 '13 at 03:43
  • possible duplcates of http://stackoverflow.com/questions/10977584/how-to-store-image-in-sqlite-database-from-gallery?rq=1 and http://stackoverflow.com/questions/8359820/saving-an-image-on-sqlite-from-camera-or-gallery?lq=1 – kalyan pvs Dec 14 '13 at 04:14
  • The Big Nerd Ranch Android book has you create an app that does some of what you want. You download bitmaps from Flickr and display them in a GridView. There is a lot of good stuff to learn in that app and the other apps in the book. – Rick Falck Dec 14 '13 at 06:40

1 Answers1

0

Saving a reference to the image resource in the database is the way I would handle it.

eimmer
  • 319
  • 1
  • 3
  • 13