dimanche 29 mars 2015

Android populate custom ListView from SQLite database

I know how to manage listview with custom adapter. I would populate my listview fron SQLite Database. This is my CartHandler:



public class CartHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

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

// Contacts table name
private static final String TABLE_PRODUCTS = "products";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_NO = "number";
private static final String KEY_PIECES = "pieces";
private static final String KEY_PRICE = "price";
private static final String KEY_TOT_PRICE = "totprice";


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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_NO + " TEXT," + KEY_PIECES + " TEXT,"+ KEY_PRICE + " TEXT," + KEY_TOT_PRICE + " TEXT" + ")";
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 " + TABLE_PRODUCTS);

// Create tables again
onCreate(db);
}


// Adding new contact
void addProduct(CartRow product) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, product.getName()); // Contact Name
values.put(KEY_NO, product.getNumber()); // Contact Phone
values.put(KEY_PIECES, product.getPieces());
values.put(KEY_PRICE, product.getPrice());
values.put(KEY_TOT_PRICE, product.getTotPrice());
// Inserting Row
db.insert(TABLE_PRODUCTS, null, values);
db.close(); // Closing database connection
}

// Getting single contact
CartRow getProduct(int id) {
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_PRODUCTS, new String[] { KEY_ID,
KEY_NAME, KEY_NO, KEY_PIECES, KEY_PRICE, KEY_TOT_PRICE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();

CartRow product = new CartRow(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), Integer.parseInt(cursor.getString(2)), Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)));
// return contact
return product;
}

// Getting All Contacts
public List<CartRow> getAllProducts() {
List<CartRow> productList = new ArrayList<CartRow>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_PRODUCTS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
CartRow product = new CartRow();
product.setID(Integer.parseInt(cursor.getString(0)));
product.setName(cursor.getString(1));
product.setNumber(Integer.parseInt(cursor.getString(2)));
product.setPieces(Integer.parseInt(cursor.getString(3)));
product.setPrice(Integer.parseInt(cursor.getString(4)));
product.setTotPrice(Integer.parseInt(cursor.getString(5)));
// Adding contact to list
productList.add(product);
} while (cursor.moveToNext());
}

// return contact list
return productList;
}

// Updating single contact
public int updateProduct(CartRow product) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, product.getName());
values.put(KEY_NO, product.getNumber());
values.put(KEY_PIECES, product.getPieces());
values.put(KEY_PRICE, product.getPrice());
values.put(KEY_TOT_PRICE, product.getTotPrice());

// updating row
return db.update(TABLE_PRODUCTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(product.getID()) });
}

// Deleting single contact
public void deleteProduct(CartRow product) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_PRODUCTS, KEY_ID + " = ?",
new String[] { String.valueOf(product.getID()) });
db.close();
}


// Getting contacts Count
public int getProductsCount() {
String countQuery = "SELECT * FROM " + TABLE_PRODUCTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();

// return count
return cursor.getCount();
}}


This is my CartRow class:



public class CartRow {

//private variables
int _id;
String _name;
int _number;
int _pieces;
int _price;
int _totprice;

// Empty constructor
public CartRow(){

}
// constructor
public CartRow(int id, String name, int number, int pieces, int price, int totprice){
this._id = id;
this._name = name;
this._number = number;
this._pieces = pieces;
this._price = price;
this._totprice = totprice;
}

// constructor
public CartRow(String name, int number, int pieces, int price, int totprice){
this._name = name;
this._number = number;
this._pieces = pieces;
this._price = price;
this._totprice = totprice;
}
// getting ID
public int getID(){
return this._id;
}

// setting id
public void setID(int id){
this._id = id;
}

public String getName(){
return this._name;
}

public void setName(String name){
this._name = name;
}

public int getNumber(){
return this._number;
}

public void setNumber(int number){
this._number = number;
}

public int getPieces(){
return this._pieces;
}

public void setPieces(int pieces){
this._pieces = pieces;
}

public int getPrice(){
return this._price;
}

public void setPrice(int price){
this._price = price;
}

public int getTotPrice(){
return this._totprice;
}

public void setTotPrice(int totprice){
this._totprice = totprice;
}}


And this is the layout I want to use in my listview.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:background="@android:color/white"
android:paddingBottom="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/number"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@android:color/black" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/name"
android:layout_alignBottom="@+id/number"
android:layout_toRightOf="@+id/number"
android:layout_toEndOf="@+id/number"
android:layout_marginLeft="25dp"
android:layout_marginStart="29dp"
android:textColor="@android:color/black" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/pieces"
android:layout_below="@+id/name"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/price"
android:layout_below="@+id/pieces"
android:layout_alignLeft="@+id/pieces"
android:layout_alignStart="@+id/pieces" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/totprice"
android:layout_alignTop="@+id/name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#ffe20c18" />
</RelativeLayout>


I correctly populate the db and read the content in the LogCat. Don't know how populate my listview. Tried to look at similar questions but found how to populate with a single information. I should already have most of code I need to let it work, I would not change the whole code to follow a sample tutorial. Can anyone help me? Any suggestion would be much appreciated. Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire