Android Open Source - ActiveAndroidDemo Product






From Project

Back to project page ActiveAndroidDemo.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project ActiveAndroidDemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gannon.activeandroiddemo.models;
/*from  w  w w  .  j a  v a 2  s. co  m*/
import com.activeandroid.ActiveAndroid;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;

import java.text.NumberFormat;
import java.util.List;

/**
 * Product Table Model
 */
@Table(name = "Products")
public class Product extends Model
{
    @Column(index = true) // index this column for faster searching by title
    public String title;
    @Column
    public String description;
    @Column
    public double price;

    @Column(name = "category_id", onDelete = Column.ForeignKeyAction.SET_NULL) // explicitly named column
                                                                               // when reference is deleted set to null
    public Category category;

    /**
     * Constructor
     */
    public Product() // super call required
    {
        super();
    }

    /**
     * Constructor
     * @param category Category to associate with
     */
    public Product(Category category) // multiple constructors may be used to add associations on creation
    {
        this();
        this.category = category;
    }

    /**
     * Gets price value as currency
     * @return String currency price
     */
    public String getPriceAsCurrency() // formatting shortcuts are often implemented in models
    {
        return NumberFormat
                .getCurrencyInstance()
                .format(price);
    }

    /**
     * Get all products
     * @return List of Product products
     */
    public static List<Product> getAll()
    {

        return new Select()
                .all()
                .from(Product.class)
                .execute();
    }

    /**
     * Deletes all products
     */
    public static void deleteAll()
    {
        new Delete()
                .from(Product.class)
                .execute();

        ActiveAndroid.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = 'Products'");
    }
}




Java Source Code List

com.gannon.activeandroiddemo.ActiveAndroidDemo.java
com.gannon.activeandroiddemo.ApplicationTest.java
com.gannon.activeandroiddemo.activities.Main.java
com.gannon.activeandroiddemo.adapters.ProductArrayAdapter.java
com.gannon.activeandroiddemo.fragments.AddProductDialogFragment.java
com.gannon.activeandroiddemo.helpers.ModelHelper.java
com.gannon.activeandroiddemo.models.Category.java
com.gannon.activeandroiddemo.models.Product.java