-
Notifications
You must be signed in to change notification settings - Fork 1
Modal
nmc9 edited this page Apr 30, 2018
·
1 revision
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;
}
The constructor must call the super constructor using the parameter context and with two other parameters
- table name
- table id
public UserModal(Context context) {
super(context, UsersTable.TABLE_NAME, UsersTable.COL_ID);
}
All three of these variables are available globally
This is where the use of this class comes in. You want to create methods that will return one of three things
- An Item (User,etc.)
- A list of Items (User[], List,etc.)
The standard procedure to get an Item or item List is the following
- Create a DatabaseHelper Object
- Create a Query Object. see Query
- Use one of the DatabaseHelper Methods to run the Query and return a MegaCursor
- Put this Cursor in the constructor of an Item.
- If getting a list loop though the list to create a list of items
- return
- A piece to build a query (ProjectionList,Where, WhereClump,etc.) see QueryBuilder