Skip to content
nmc9 edited this page Apr 30, 2018 · 1 revision

Item

The item class should look like the table class. It should have all the variables you want to use in your activities. This call should be used by your activities to get data from the database in the format of this item.

Example

public class User extends Item {

    public Integer userid;
    public String username;
    public String password;

    public static UserModal getModal(Context mContext) {
        return new UserModal(mContext);
    }

    public User() {

    }
     
    public User(MegaCursor cursor) {
        super();
        userid = cursor.getIntByField(UsersTable.COL_ID);
        username = cursor.getStringByField(UsersTable.COL_USERNAME);
        password = cursor.getStringByField(UsersTable.COL_PASSWORD);
        blob = cursor.getStringByField(UsersTable.FIELD_BLOB);
        dub = cursor.getDoubleByField(UsersTable.FIELD_DOUBLE);
        date = DateTimeConversion.StringToDate(cursor.getStringByField(UsersTable.FIELD_DATETIME));
    }

    @Override
    public String dump() {
        String dump = "User dump:\n";
        dump += "userid" + userid + "\n";
        dump += "username" + username + "\n";
        dump += "password" + password + "\n";
        return dump;
    }

    @Override
    public ContentValues getRecord() {
        ContentValues cv = new ContentValues();
        cv.put(UsersTable.COL_ID, userid);
        cv.put(UsersTable.COL_USERNAME, username);
        cv.put(UsersTable.COL_PASSWORD, password);
        return cv;
    }

    @Override
    public boolean insert(Context context) {
        DatabaseHelper db = new Database(context);
        boolean i = insertWithId(db, UsersTable.TABLE_NAME, UsersTable.COL_ID, getRecord());
        db.close();
        return i;
    }

    @Override
    public boolean update(Context context) {
        DatabaseHelper db = new Database(context);
        boolean i = update(db, UsersTable.TABLE_NAME, UsersTable.COL_ID, getRecord(), userid);
        db.close();
        return i;
    }

    @Override
    public boolean delete(Context context, int mode) {
        DatabaseHelper db = new Database(context);
        boolean i = delete(db, UsersTable.TABLE_NAME, UsersTable.COL_ID, userid);
        db.close();
        return i;
    }
}

Variables and Methods

static getModal:

It's usefull to add a static getModal(Context context) method so you can access the related modal from Item classes.

Constructor(s):

There are two constructors. One that builds a blank Item and one that builds an Item from a MegaCursor (More info in QueryBuilder). In this constructor set the Item class variables with this cursor using the stings from your Table class. It should look something like this varaible = cursor.getIntByField(ClassTable.FIELD_NAME);

getRecord:

The getRecord method is used for making Insert/Update Operations. This should do the reverse of the constructor. Create a ContentValue object and fill it with the variables of this class. It should look something like this contentvalue.put(ClassTable.COLUMN_NAME, variable); The dump method is mainly used for testing. Plug values in to return a String to Log data

Insert/Update/Delete

There are three other methods Insert,Update,Delete. They do what the name implies. Use these methods to call helper methods that will do the work of these methods for you. Follow the example for an idea but each of these methods should have the same flow.

  1. Create DatabaseHelper Object
  2. Pass data to helper function
  3. close DatabaseHelper object;
  4. return result.

There are 4 helper functions insert,update,delete,insertWithId

If the table has an Auto_Increment id field use insert otherwise insertWithID They operate the same otherwise

insert() parameters

  1. DatabaseHelper object
  2. The name of the table being inserted
  3. The name of the id column
  4. Content Values retrieved by getRecord();

update() parameters

  1. DatabaseHelper object
  2. The name of the table being inserted
  3. The name of the id column
  4. Content Values retrieved by getRecord();
  5. the id column value of this record

delete() parameters

  1. DatabaseHelper object
  2. The name of the table being inserted
  3. The name of the id column
  4. the id column value of this record

insertWithId() parameters

  1. DatabaseHelper object
  2. The name of the table being inserted
  3. The name of the id column
  4. Content Values retrieved by getRecord();