Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.rayan.mydb;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import com.example.rayan.mydb.MyClasses.*;
- import java.util.ArrayList;
- public class RZDatabaseManager {
- // the Activity or Application that is creating an object from this class.
- Context context;
- // a reference to the database used by this application/object
- public SQLiteDatabase db;
- // These constants are specific to the database. They should be
- // changed to suit your needs.
- private final String DB_NAME = "RZ_DB";
- private final int DB_VERSION = 01;
- private String[] user_fields = new String[] { "_id", "name", "username",
- "password", "is_admin" };
- private final String TABLE_USER = "user";
- private String[] test_fields = new String[] { "_id", "test_name",
- "type_id", "max_score", "level", "chapter_id" };
- private final String TABLE_TEST = "test";
- private String[] chapter_fields = new String[] { "_id", "name", "body" };
- private final String TABLE_CHAPTER = "chapter";
- private String[] question_fields = new String[] { "_id", "body", "test_id" };
- private final String TABLE_QUESTION = "question";
- private String[] options_fields = new String[] { "_id", "body",
- "question_id", "is_correct" };
- private final String TABLE_OPTIONS = "options";
- private String[] test_type_fields = new String[] { "_id", "type" };
- private final String TABLE_TEST_TYPE = "test_type";
- private String[] test_taker_fields = new String[] { "_id", "student_id",
- "test_id", "date", "score" };
- private final String TABLE_TEST_TAKER = "test_taker";
- public RZDatabaseManager(Context context) {
- this.context = context;
- // create or open the database
- CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
- this.db = helper.getWritableDatabase();
- }
- /**
- * @author Rayan Zahab
- * @param db
- * handler
- * @param tableName
- * table to create
- * @param fields
- * fields of the table
- */
- public void createTable(SQLiteDatabase db, String tableName, String[] fields) {
- String query = "create table " + tableName + " (";
- for (int position = 0; position < fields.length; position++) {
- String field = fields[position];
- if (position == 0) {
- query = query + field
- + " integer primary key autoincrement not null, ";
- } else if (position != fields.length - 1) {
- query = query + field + " text,";
- } else {
- query = query + field + " text" + ");";
- }
- }
- db.execSQL(query);
- }
- private class CustomSQLiteOpenHelper extends SQLiteOpenHelper {
- public CustomSQLiteOpenHelper(Context context) {
- super(context, DB_NAME, null, DB_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- createTable(db, TABLE_USER, user_fields);
- createTable(db, TABLE_QUESTION, question_fields);
- createTable(db, TABLE_OPTIONS, options_fields);
- createTable(db, TABLE_CHAPTER, chapter_fields);
- createTable(db, TABLE_TEST, test_fields);
- createTable(db, TABLE_TEST_TYPE, test_type_fields);
- createTable(db, TABLE_TEST_TAKER, test_taker_fields);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- }
- public void createAdmin() {
- String name = "Admin";
- String username = "admin";
- String pass = "admin";
- String pass2 = "admin";
- User u = new User(name, username, pass, pass2);
- u.setAdmin(true);
- insertRow(u);
- }
- /**
- * This function s rows: It knws to which table to save according to
- * the type of the class sent
- *
- * @author Rayan Zahab <rayanzahab@gmail.com>
- */
- public int insertRow(Object o) {
- String[] fields = new String[] {};
- String[] values = new String[] {};
- String tableName = "";
- String cl_name = o.getClass().getSimpleName();
- if (cl_name.equals("User")) {
- fields = user_fields;
- tableName = TABLE_USER;
- User u = (User) o;
- values = new String[] { u.getName(), u.getUsername(),
- u.getPassword(), String.valueOf(u.isAdmin()) };
- }
- else if (cl_name.equals("Chapter")) {
- fields = chapter_fields;
- tableName = TABLE_CHAPTER;
- Chapter c = (Chapter) o;
- values = new String[] { c.getName(), c.getBody() };
- }
- else if (cl_name.equals("TestType")) {
- fields = test_type_fields;
- tableName = TABLE_TEST_TYPE;
- TestType c = (TestType) o;
- values = new String[] { c.getType() };
- }
- else if (cl_name.equals("Test")) {
- fields = test_fields;
- tableName = TABLE_TEST;
- Test c = (Test) o;
- values = new String[] { "" + c.getName(), "" + c.getType_id(),
- "" + c.getMaxScore(), "" + c.getLevel(),
- "" + c.getChapter_id() };
- }
- else if (cl_name.equals("Question")) {
- fields = question_fields;
- tableName = TABLE_QUESTION;
- Question c = (Question) o;
- values = new String[] { c.getBody(), "" + c.getTest_id() };
- }
- else if (cl_name.equals("Option")) {
- fields = options_fields;
- tableName = TABLE_OPTIONS;
- Option c = (Option) o;
- values = new String[] { c.getBody(), c.getQuestion_id() + "",
- new Boolean(c.isCorrect()).toString() };
- }
- else if (cl_name.equals("TestTaker")) {
- fields = test_taker_fields;
- tableName = TABLE_TEST_TAKER;
- TestTaker c = (TestTaker) o;
- values = new String[] { c.getUser_id()+"", c.getTest_id() + "",c.getDate()+ "",c.getScore() + ""};
- }
- ContentValues content = new ContentValues();
- for (int position = 1; position < fields.length; position++) {
- String field = fields[position];
- String value = values[position - 1];
- content.put(field, value);
- }
- try {
- return (int) db.insert(tableName, null, content);
- } catch (Exception e) {
- Log.e("DB ERROR", e.toString());
- e.printStackTrace();
- }
- return 0;
- }
- /**
- * UPDATING A ROW IN THE DATABASE TABLE
- *
- * @author Rayan Zahab & andreh abboud
- * @param o
- */
- public void deleteRow(Object o) {
- String tableName = "";
- int id = 0;
- String cl_name = o.getClass().getSimpleName();
- /*
- * if (cl_name.equals("User")) { tableName = "user"; User u = (User) o;
- * id = u.getID(); try { db.delete(TABLE_RESERVATION, " user =" + id,
- * null); } catch (Exception e) { Log.e("DB ERROR", e.toString());
- * e.printStackTrace(); } } else if (cl_name.equals("Room")) { tableName
- * = TABLE_ROOM; Room r = (Room) o; id = r.getId(); try {
- * db.delete(TABLE_RESERVATION, " room =" + id, null); } catch
- * (Exception e) { Log.e("DB ERROR", e.toString()); e.printStackTrace();
- * } } else if (cl_name.equals("Reservation")) { tableName =
- * TABLE_RESERVATION; Reservation res = (Reservation) o; id =
- * res.getId(); }
- */
- // ask the database manager to delete the row of given id
- try {
- db.delete(tableName, " _id =" + id, null);
- } catch (Exception e) {
- Log.e("DB ERROR", e.toString());
- e.printStackTrace();
- }
- }
- /**
- * UPDATING A ROW IN THE DATABASE TABLE
- *
- * @author Rayan Zahab
- * @param o
- * to edit having the old id, and new values
- */
- public void updateRow(Object o) {
- String[] fields = new String[] {};
- String[] values = new String[] {};
- String tableName = "";
- int id = 0;
- String cl_name = o.getClass().getSimpleName();
- /*
- * if (cl_name.equals("User")) { fields = user_fields; tableName =
- * "user"; User u = (User) o; id = u.getID(); values = new String[] {
- * u.getFullName(), u.getEmail(), u.getCompany(), u.getPhoneNumber(),
- * u.getPassword(), u.getIsAdmin() }; } else if (cl_name.equals("Room"))
- * { fields = room_fields; tableName = "room"; Room r = (Room) o; id =
- * r.getId(); values = new String[] { r.getName(), r.getAddress(),
- * r.getCapacity(), r.getDescription() }; } else if
- * (cl_name.equals("Reservation")) { fields = reservation_fields;
- * tableName = "reservation"; Reservation res = (Reservation) o; Date
- * date = res.getDate(); String date_str = date.toString(); values = new
- * String[] { date_str, res.getFrom(), res.getTo(), "" +
- * res.getRoomId(), "" + res.getUserId() }; }
- */
- ContentValues content = new ContentValues();
- for (int position = 1; position < fields.length; position++) {
- String field = fields[position];
- String value = values[position - 1];
- content.put(field, value);
- }
- // ask the database object to update the database row of given rowID
- try {
- db.update(tableName, content, "_id" + "=" + id, null);
- } catch (Exception e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- }
- /**
- * @author Rayan Zahab RETRIEVING A ROW FROM A DATABASE TABLE
- * @param rowID
- * : the id of the row to retrieve, tableName :the table from
- * which to get the row, fields : the fields of the table
- * @return an array containing the data from the row
- */
- public ArrayList<Object> getRowAsArray(long rowID, String tableName,
- String[] fields) {
- ArrayList<Object> rowArray = new ArrayList<Object>();
- Cursor cursor;
- try {
- cursor = db.query(tableName, fields, fields[0] + "=" + rowID, null,
- null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- for (int position = 0; position < fields.length; position++) {
- rowArray.add(cursor.getString(position));
- }
- } while (cursor.moveToNext());
- }
- cursor.close();
- } catch (SQLException e) {
- Log.e("DB ERROR", e.toString());
- e.printStackTrace();
- }
- return rowArray;
- }
- //andreh abboud
- /**
- *
- * Check if the login info are right to allow login
- *
- * @author Rayan Zahab andreh abboud
- * @param user
- * @return
- */
- public User getRowAsArrayForLogin(User user) {
- String[] fields = user_fields;
- String tableName = "user";
- ArrayList<String> rowArray = new ArrayList<String>();
- User userReturn = null;
- Cursor cursor;
- try {
- cursor = db.query(tableName, fields, fields[2] + " LIKE " + "'"
- + user.getUsername() + "' AND " + fields[3] + " LIKE "
- + "'" + user.getPassword() + "'", null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- userReturn = new User();
- do {
- for (int position = 0; position < fields.length; position++) {
- rowArray.add(cursor.getString(position));
- }
- } while (cursor.moveToNext());
- userReturn.setId(Integer.parseInt(rowArray.get(0)));
- userReturn.setName(rowArray.get(1));
- userReturn.setUsername(rowArray.get(2));
- userReturn.setAdmin(Boolean.parseBoolean(rowArray.get(4)));
- userReturn.setPassword(rowArray.get(3));
- }
- cursor.close();
- } catch (SQLException e) {
- Log.e("DB ERROR", e.toString());
- e.printStackTrace();
- }
- return userReturn;
- }
- /**
- *
- * Get rows having param=passed value in the paased table
- *
- * @author Rayan Zahab
- * @param param
- * : the position of the attribute to companre in the table
- * fields
- * @param tableName
- * @param value
- * is the value to search for
- * @return
- */
- public ArrayList<ArrayList<Object>> getRowAsArrayByPosition(int param,
- String tableName, String value) {
- String[] fields = user_fields;
- /*
- * if (tableName.equals("User")) { fields = user_fields;
- *
- * } else if (tableName.equals("Room")) { fields = room_fields; } else
- * if (tableName.equals("Reservation")) { fields = reservation_fields; }
- */
- ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
- Cursor cursor;
- try {
- cursor = db.query(tableName, fields, fields[param] + " LIKE '"
- + value + "'", null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- ArrayList<Object> dataList = new ArrayList<Object>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- dataArrays.add(dataList);
- } while (cursor.moveToNext());
- }
- cursor.close();
- } catch (SQLException e) {
- Log.e("DB ERROR", e.toString());
- e.printStackTrace();
- }
- return dataArrays;
- }
- /**
- *
- * Get attribute at position ATT having attribute at position param in
- * tableName
- *
- * @author Rayan Zahab
- * @param param
- * : the position of the attribute to companre in the table
- * fields
- * @param param
- * : the position of the attribute to get it value
- * @param tableName
- * @param value
- * is the value to search for
- * @return
- */
- public String getValueByParam(String tableName, int att, int param,
- String value) {
- String[] fields = user_fields;
- if (tableName.equals("User")) {
- fields = user_fields;
- tableName = TABLE_USER;
- } else if (tableName.equals("TestType")) {
- fields = test_type_fields;
- tableName = TABLE_TEST_TYPE;
- } else if (tableName.equals("Chapter")) {
- fields = chapter_fields;
- tableName = TABLE_CHAPTER;
- } else if (tableName.equals("Test")) {
- fields = test_fields;
- tableName = TABLE_TEST;
- } else if (tableName.equals("Question")) {
- fields = question_fields;
- tableName = TABLE_QUESTION;
- } else if (tableName.equals("Option")) {
- fields = options_fields;
- tableName = TABLE_OPTIONS;
- }
- Cursor cursor;
- String toReturn = "";
- try {
- cursor = db.query(tableName, fields, fields[param] + " LIKE '"
- + value + "'", null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- toReturn = cursor.getString(att);
- cursor.close();
- return toReturn;
- } while (cursor.moveToNext());
- }
- cursor.close();
- } catch (SQLException e) {
- Log.e("DB ERROR", e.toString());
- e.printStackTrace();
- }
- return toReturn;
- }
- public String getValueByTParam(String tableName, int att, int param1, int param2,
- String value1,String value2) {
- String[] fields = user_fields;
- if (tableName.equals("User")) {
- fields = user_fields;
- tableName = TABLE_USER;
- } else if (tableName.equals("TestType")) {
- fields = test_type_fields;
- tableName = TABLE_TEST_TYPE;
- } else if (tableName.equals("Chapter")) {
- fields = chapter_fields;
- tableName = TABLE_CHAPTER;
- } else if (tableName.equals("Test")) {
- fields = test_fields;
- tableName = TABLE_TEST;
- } else if (tableName.equals("Question")) {
- fields = question_fields;
- tableName = TABLE_QUESTION;
- } else if (tableName.equals("Option")) {
- fields = options_fields;
- tableName = TABLE_OPTIONS;
- }
- Cursor cursor;
- String toReturn = "";
- try {
- cursor = db.query(tableName, fields, fields[param1] + " LIKE '"
- + value1 + "' AND "+fields[param2]+" LIKE '"+value2+"'", null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- toReturn = cursor.getString(att);
- cursor.close();
- return toReturn;
- } while (cursor.moveToNext());
- }
- cursor.close();
- } catch (SQLException e) {
- Log.e("DB ERROR", e.toString());
- e.printStackTrace();
- }
- return toReturn;
- }
- /**
- * Checks if a username (email) already exists in the db
- *
- * @author Rayan Zahab
- * table fields
- * @return
- */
- public ArrayList<String> getRowAsArrayByUser(Object u) {
- ArrayList<String> rowArray = new ArrayList<String>();
- Cursor cursor;
- String[] fields = user_fields;
- String tableName = "user";
- String email = "";// u.getEmail();
- try {
- cursor = db.query(tableName, fields, fields[2] + " LIKE " + "'"
- + email + "'", null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- for (int position = 0; position < fields.length; position++) {
- rowArray.add(cursor.getString(position));
- }
- } while (cursor.moveToNext());
- }
- cursor.close();
- } catch (SQLException e) {
- Log.e("DB ERROR", e.toString());
- e.printStackTrace();
- }
- return rowArray;
- }
- /**
- * RETRIEVING ALL ROWS FROM A DATABASE TABLE
- *
- * @author Rayan Zahab
- * the fields of the table
- * @return an array containing the data of the table
- *
- */
- // use this to list chapters!!!!
- public ArrayList<ArrayList<Object>> getTablesRowsAsArrays(Object o) {
- String tableName = "";
- String[] fields = new String[] {};
- String cl_name = o.getClass().getSimpleName();
- if (cl_name.equals("User")) {
- fields = user_fields;
- tableName = "user";
- } else if (cl_name.equals("Chapter")) {
- fields = chapter_fields;
- tableName = "chapter";
- }
- ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
- Cursor cursor;
- try {
- cursor = db.query(tableName, fields, null, null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- ArrayList<Object> dataList = new ArrayList<Object>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- dataArrays.add(dataList);
- } while (cursor.moveToNext());
- }
- } catch (SQLException e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- return dataArrays;
- }
- public TestSet getChapterTestByType(int chapterId, int typeId) {
- String tableName = "test";
- String[] fields = test_fields;
- Cursor cursor;
- try {
- cursor = db.query(tableName, fields, fields[5] + " LIKE "
- + chapterId + " AND " + fields[2] + " LIKE " + typeId,
- null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- Test testTmp = new Test();
- do {
- ArrayList<String> dataList = new ArrayList<String>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- testTmp.setId(Integer.parseInt(dataList.get(0)));
- testTmp.setType_id(Integer.parseInt(dataList.get(2)));
- testTmp.setMaxScore(Integer.parseInt(dataList.get(3)));
- testTmp.setLevel(Integer.parseInt(dataList.get(4)));
- testTmp.setChapter_id(Integer.parseInt(dataList.get(5)));
- TestSet setTmp = new TestSet();
- setTmp.setTest(testTmp);
- setTmp.setQuestions(getQuestions(testTmp.getId()));
- return setTmp;
- } while (cursor.moveToNext());
- }
- } catch (SQLException e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- return null;
- }
- public ArrayList<Chapter> getChapters(int chId) {
- String tableName = "chapter";
- String[] fields = chapter_fields;
- ArrayList<Chapter> myReturn= new ArrayList<Chapter>();
- Cursor cursor;
- try {
- String where = "";
- if(chId!=0)
- {
- where = fields[0]+" LIKE "+chId;
- }
- cursor = db.query(tableName, fields, where,
- null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- Chapter chTmp= new Chapter();
- ArrayList<String> dataList = new ArrayList<String>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- chTmp.setId(Integer.parseInt(dataList.get(0)));
- chTmp.setName(dataList.get(1));
- chTmp.setBody(dataList.get(2));
- myReturn.add(chTmp);
- } while (cursor.moveToNext());
- }
- } catch (SQLException e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- return myReturn;
- }
- public ArrayList<Question> getQuestions(int testId) {
- String tableName = "";
- String[] fields = new String[] {};
- ArrayList<Question> myQsts = new ArrayList<Question>();
- fields = question_fields;
- tableName = "question";
- Cursor cursor;
- try {
- cursor = db.query(tableName, fields, fields[2] + " LIKE " + testId,
- null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- int i=0;
- do {
- Question qstTmp = new Question();
- ArrayList<String> dataList = new ArrayList<String>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- qstTmp.setId(Integer.parseInt(dataList.get(0)));
- qstTmp.setBody(dataList.get(1));
- qstTmp.setTest_id(Integer.parseInt(dataList.get(2)));
- qstTmp.setMyOptions(getOptions(qstTmp.getId()));
- myQsts.add(i, qstTmp);
- i++;
- } while (cursor.moveToNext());
- }
- } catch (SQLException e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- return myQsts;
- }
- public ArrayList<Option> getOptions(int questId) {
- String tableName = "";
- String[] fields = new String[] {};
- ArrayList<Option> myOpts = new ArrayList<Option>();
- fields = options_fields;
- tableName = "options";
- Cursor cursor;
- try {
- cursor = db.query(tableName, fields,
- fields[2] + " LIKE " + questId, null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- Option optTmp = new Option();
- ArrayList<String> dataList = new ArrayList<String>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- optTmp.setId(Integer.parseInt(dataList.get(0)));
- optTmp.setBody(dataList.get(1));
- optTmp.setQuestion_id(Integer.parseInt(dataList.get(2)));
- optTmp.setCorrect(new Boolean(dataList.get(3)));
- myOpts.add(optTmp);
- } while (cursor.moveToNext());
- }
- } catch (SQLException e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- return myOpts;
- }
- public ArrayList<ArrayList<Object>> getTest(String name) {
- String[] fields = new String[] {};
- String cl_name = "Test";
- String tableName = "";
- fields = test_fields;
- tableName = "test";
- ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
- Cursor cursor;
- try {
- cursor = db.query(tableName, fields, fields[1] + " LIKE " + name,
- null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- ArrayList<Object> dataList = new ArrayList<Object>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- dataArrays.add(dataList);
- } while (cursor.moveToNext());
- }
- } catch (SQLException e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- return dataArrays;
- }
- public ArrayList<TestTaker> getUserTests(int uId) {
- String tableName = "test_taker";
- String[] fields = test_taker_fields;
- ArrayList<TestTaker> myReturn= new ArrayList<TestTaker>();
- Cursor cursor;
- try {
- String where = "";
- if(uId!=0)
- {
- where = fields[1]+" LIKE "+uId;
- }
- cursor = db.query(tableName, fields, where,
- null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- TestTaker uTmp= new TestTaker();
- ArrayList<String> dataList = new ArrayList<String>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- uTmp.setId(Integer.parseInt(dataList.get(0)));
- uTmp.setUser_id(Integer.parseInt(dataList.get(1)));
- uTmp.setTest_id(Integer.parseInt(dataList.get(2)));
- uTmp.setScore(Integer.parseInt(dataList.get(4)));
- uTmp.setDate(dataList.get(3));
- myReturn.add(uTmp);
- } while (cursor.moveToNext());
- }
- } catch (SQLException e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- return myReturn;
- }
- public ArrayList<User> getUsers(int uId) {
- String tableName = "user";
- String[] fields = user_fields;
- ArrayList<User> myReturn= new ArrayList<User>();
- Cursor cursor;
- try {
- String where = "";
- if(uId!=0)
- {
- where = fields[0]+" LIKE "+uId;
- }
- cursor = db.query(tableName, fields, where,
- null, null, null, null);
- cursor.moveToFirst();
- if (!cursor.isAfterLast()) {
- do {
- User uTmp= new User();
- ArrayList<String> dataList = new ArrayList<String>();
- for (int position = 0; position < fields.length; position++) {
- dataList.add(cursor.getString(position));
- }
- uTmp.setId(Integer.parseInt(dataList.get(0)));
- uTmp.setName(dataList.get(1));
- uTmp.setUsername(dataList.get(2));
- uTmp.setPassword(dataList.get(3));
- myReturn.add(uTmp);
- } while (cursor.moveToNext());
- }
- } catch (SQLException e) {
- Log.e("DB Error", e.toString());
- e.printStackTrace();
- }
- return myReturn;
- }
- /**
- * This function return the a db object based on its position
- *
- * @author Rayan Zahab
- *
- * @param position
- * position of the element to get (usually given based on user
- * selection)
- * @param o
- * o this will be from type user or room or reservation. And will
- * be processed based on the type in the getTablesRowsAsArrays()
- *
- * @return ArrayList<Object>
- */
- public ArrayList<Object> getObjectFromPosition(int position, Object o) {
- ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
- // Get all data in the db by passing the object
- dataArrays = this.getTablesRowsAsArrays(o);
- // get the object at the needed position
- ArrayList<Object> row = dataArrays.get(position);
- return row;
- }
- public void testType() {
- ArrayList<Object> tt = new ArrayList<Object>();
- tt.add(new TestType("TF"));
- tt.add(new TestType("MCQ"));
- tt.add(new TestType("OP"));
- for (int i = 0; i < tt.size(); i++) {
- insertRow(tt.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement