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

Modal

This class is where you build all your queries using QueryBuilder. Make sure to extend the ItemModal class

## Example 
public class UserModal extends ItemModal {
    public UserModal(Context context) {
        super(context, UsersTable.TABLE_NAME, UsersTable.COL_ID);
    }

    public List<User> getListOfUsers() {
        DatabaseHelper db = new Database(mContext);
        Query query = new Query(null, new JoinHelper(table), null, null);
        MegaCursor cursor = db.megaSelect(query);
        List<User> users = new ArrayList<>();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            User user = new User(cursor);
            users.add(user);
            cursor.moveToNext();
        }
        return users;
    }

    public User getUser(int user_id) {
        DatabaseHelper db = new Database(mContext);
        Where where = new Where(UserTable.COL_ID,"=",user_id);
        Query query = new Query(null, new JoinHelper(table), where, null);
        MegaCursor cursor = db.megaSelect(query);
        cursor.moveToFirst();
        if(!cursor.empty()) {
            return new User(cursor);
        }
        return null;
    }


Variables and Methods

Constructor:

The constructor must call the super constructor using the parameter context and with two other parameters

  1. table name
  2. table id
    public UserModal(Context context) {
        super(context, UsersTable.TABLE_NAME, UsersTable.COL_ID);
    }

All three of these variables are available globally

Custom Methods

This is where the use of this class comes in. You want to create methods that will return one of three things

  1. An Item (User,etc.)
  2. A list of Items (User[], List,etc.)

Standard procedure

The standard procedure to get an Item or item List is the following

  1. Create a DatabaseHelper Object
  2. Create a Query Object. see Query
  3. Use one of the DatabaseHelper Methods to run the Query and return a MegaCursor
  4. Put this Cursor in the constructor of an Item.
  5. If getting a list loop though the list to create a list of items
  6. return
  1. A piece to build a query (ProjectionList,Where, WhereClump,etc.) see QueryBuilder