lundi 7 septembre 2015

Having trouble marking the correct notification as "read" in my notification adapter

This problem has been stumping me for days. I can't seem to get the correct notfication box to be marked as "read" from "unread" in both the local NotificationModels list and the SQLite Database. When I run the program everything works fine and when the mNotificationType is clicked on the program correctly changes the menu title and loads the correct submenu but when I go back to the notifications the wrong notifications are marked as read. How do I get the proper index of the clicked item?

NotificationActivity:

public class NotificationActivity extends BaseActivity implements SQLiteAdapter.SQLiteDbQueryListener, SimpleGestureFilter.SimpleGestureListener {
public static final String TAG = LoginActivity.class.getSimpleName();
private NotificationAdapter notificationAdapter;
private HeaderLayout headerLayout;
private FooterLayout footerLayout;
private SimpleGestureFilter detector;
private SQLiteAdapter sqLiteAdapter;
private ListView mNotificationLv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);
    mNotificationLv = (ListView) findViewById(R.id.notification_lv);

    sqLiteAdapter = new SQLiteAdapter(this, this, DialogUtil.createProgressDialog(this));
    headerLayout = new HeaderLayout(this);
    footerLayout = new FooterLayout(this);
    footerLayout.setNotificationMode();

    mNotificationLv = (ListView) findViewById(R.id.notification_lv);

    List<NotificationModel> userModelList = sqLiteAdapter.getNotificationList();

    //Default Model Screen
    /*userModelList.add(new NotificationModel("traffic", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "3 minutes ago", "unread"));
    userModelList.add(new NotificationModel("enforcement", "<b>Lorem Ipsum</b> have changed in your <b>current State</b>", "28 minutes ago", "unread"));
    userModelList.add(new NotificationModel("alcohal", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "2 hours ago", "read"));
    userModelList.add(new NotificationModel("taxes", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "3 hours ago", "read"));
    userModelList.add(new NotificationModel("guns", "<b>Lorem Ipsum</b> have changed in your <b>current State</b>", "18 hours ago", "read"));
    userModelList.add(new NotificationModel("marijuana", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "1 days ago", "read"));
    userModelList.add(new NotificationModel("traffic", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "3 days ago", "read"));
    userModelList.add(new NotificationModel("enforcement", "<b>Lorem Ipsum</b> have changed in your <b>current State</b>", "5 days ago", "read"));
    userModelList.add(new NotificationModel("alcohal", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "1 weeks ago", "read"));
    userModelList.add(new NotificationModel("taxes", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "1 weeks ago", "read"));
    userModelList.add(new NotificationModel("guns", "<b>Lorem Ipsum</b> have changed in your <b>current State</b>", "2 weeks ago", "read"));
    userModelList.add(new NotificationModel("marijuana", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "1 month ago", "read"));
    userModelList.add(new NotificationModel("alcohal", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "1 weeks ago", "read"));
    userModelList.add(new NotificationModel("taxes", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "1 weeks ago", "read"));
    userModelList.add(new NotificationModel("guns", "<b>Lorem Ipsum</b> have changed in your <b>current State</b>", "2 weeks ago", "read"));
    userModelList.add(new NotificationModel("marijuana", "<b>Lorem Ipsum</b> have changed in your <b>home State</b>", "1 month ago", "read"));?*/

    notificationAdapter = new NotificationAdapter(this);
    notificationAdapter.setList(userModelList);
    mNotificationLv.setAdapter(notificationAdapter);

    // Detect touched area
    detector = new SimpleGestureFilter(this,this);
}

@Override
protected void onResume() {
    super.onResume();
    FontLoader.loadFonts(this);
    headerLayout.LoadHeaderFont();
    footerLayout.LoadFooterFont();
}

public static Intent getIntent(Context context) {
    Intent intent = new Intent(context, NotificationActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    return intent;
}

//Swipe related code start here
@Override
public boolean dispatchTouchEvent(MotionEvent me){
    // Call onTouchEvent of SimpleGestureFilter class
    this.detector.onTouchEvent(me);
    return super.dispatchTouchEvent(me);
}

public void onSwipe(int direction) {
    switch (direction) {
        case SimpleGestureFilter.SWIPE_RIGHT :
            startActivityWithAnim(WindowMenuActivity.getIntent(NotificationActivity.this));
            break;
        case SimpleGestureFilter.SWIPE_LEFT :
            startActivityWithAnim(LawDataActivity.getIntent(NotificationActivity.this));
            break;
        case SimpleGestureFilter.SWIPE_DOWN :
            break;
        case SimpleGestureFilter.SWIPE_UP :
            break;
    }
}

@Override
public void onSuccess(int reqCode) {

}

@Override
public void onCancel(boolean canceled) {

}
//Swipe related code ends here
}

NotificationAdapter

public class NotificationAdapter extends BaseAdapter implements SQLiteAdapter.SQLiteDbQueryListener{
private List<NotificationModel> lstNotificationModels;
private SQLiteAdapter sqLiteAdapter;
private Context context;
private int currentPosition;

public NotificationAdapter(Context context) {
    this.context = context; sqLiteAdapter=new SQLiteAdapter(context, this, new Dialog(context));
}

public void setList(List<NotificationModel> genres) {
    this.lstNotificationModels = genres;
    notifyDataSetChanged();
}

public int getCurrentPosition(){
    return currentPosition;
}

@Override
public void onSuccess(int reqCode) {

}

@Override
public void onCancel(boolean canceled) {

}

private class ViewHolder {
    TextView mNotifiactionTypeTv, mNotifiactionTextTv, mNotifiactionTimeTv;
    LinearLayout rowNotificationLl;
}

public void setRead(int i){
    sqLiteAdapter.updateNotificationStatus(i, "read");
    lstNotificationModels.get(i).setmNotificationStatus("read");
}

@Override
public int getCount() {
    return null == lstNotificationModels ? 0 : lstNotificationModels.size();
}

@Override
public Object getItem(int position) {
    return lstNotificationModels.get(position);
}

@Override
public long getItemId(int position) {
    return lstNotificationModels.indexOf(getItem(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    currentPosition = position;
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row_notification, null);
        holder = new ViewHolder();
        holder.rowNotificationLl = (LinearLayout) convertView.findViewById(R.id.row_notification_ll);
        holder.mNotifiactionTextTv = (TextView) convertView.findViewById(R.id.notification_text_tv);
        holder.mNotifiactionTimeTv = (TextView) convertView.findViewById(R.id.notification_time_tv);
        holder.mNotifiactionTypeTv = (TextView) convertView.findViewById(R.id.notification_type_atv);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    NotificationModel notificationModel = lstNotificationModels.get(position);
    holder.mNotifiactionTextTv.setText(Html.fromHtml(notificationModel.getmNotificationText().trim()));
    holder.mNotifiactionTimeTv.setText(notificationModel.getmNotificationTime().trim());
    if (notificationModel.getmNotificationStatus().equalsIgnoreCase("unread")) {
        convertView.findViewById(R.id.unread_vw).setVisibility(View.VISIBLE);
        holder.rowNotificationLl.setBackgroundResource(R.color.black);
    }
    else {
        convertView.findViewById(R.id.unread_vw).setVisibility(View.GONE);
        holder.rowNotificationLl.setBackgroundResource(R.color.grad_light);
    }

    switch (notificationModel.getmNotificationType().toLowerCase()){
        case "traffic":
            holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.traffic_noti_bg);
            holder.mNotifiactionTypeTv.setText("f");
            holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    AtlasApplication.MenuTitle = Constants.CAT_TRAFFIC;
                    AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(0);
                    setRead(getCurrentPosition());
                    view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
                }
            });
            break;
        case "law enforcement":
            holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.enforcement_noti_bg);
            holder.mNotifiactionTypeTv.setText("c");
            holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    AtlasApplication.MenuTitle = Constants.CAT_ENFORCEMENT;
                    AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(1);
                    setRead(getCurrentPosition());
                    view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
                }
            });
            break;
        case "alcohol":
            holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.alcohal_noti_bg);
            holder.mNotifiactionTypeTv.setText("a");
            holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    AtlasApplication.MenuTitle = Constants.CAT_ALCOHOL;
                    AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(2);
                    setRead(getCurrentPosition());
                    view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
                }
            });
            break;
        case "taxes":
            holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.taxes_noti_bg);
            holder.mNotifiactionTypeTv.setText("e");
            holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    AtlasApplication.MenuTitle = Constants.CAT_TAXES;
                    AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(3);
                    setRead(getCurrentPosition());
                    view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
                }
            });
            break;
        case "guns":
            holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.guns_noti_bg);
            holder.mNotifiactionTypeTv.setText("b");
            holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    AtlasApplication.MenuTitle = Constants.CAT_GUNS;
                    AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(4);
                    setRead(getCurrentPosition());
                    view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
                }
            });
            break;
        case "marijuana":
            holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.marijuana_noti_bg);
            holder.mNotifiactionTypeTv.setText("d");
            holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    AtlasApplication.MenuTitle = Constants.CAT_MARIJUANA;
                    AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(5);
                    setRead(getCurrentPosition());
                    view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
                }
            });
            break;
        default:
            break;
    }
    FontLoader.setAtlasFont(holder.mNotifiactionTypeTv);
    FontLoader.setRalewayRegularFont(holder.mNotifiactionTextTv, holder.mNotifiactionTimeTv);

    holder.rowNotificationLl.setId(position);
    holder.rowNotificationLl.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    return convertView;
}
}

SQLiteAdaptor:

public class SQLiteAdapter {
private static final int DATABASE_VERSION = 1;

public static SQLiteHelper sSqLiteHelper;
private static String DB_PATH;//= Environment.getExternalStorageDirectory() + "/" + context.getPackageName() + "/";
private Runnable runnable;
private SQLiteDatabase mSqLiteDatabase;
private Context mContext;
private Dialog mDialog;
private SQLiteDbQueryListener sqLiteDbQueryListener;
private ExceptionHandler exceptionHandler;

public SQLiteAdapter(Context c, SQLiteDbQueryListener listener, Dialog dialog) {
    mContext = c;
    sqLiteDbQueryListener = listener;
    exceptionHandler = new ExceptionHandler(mContext, "SQLiteAdapter");
    mDialog = dialog;
    DB_PATH = Environment.getExternalStorageDirectory() + "/" + mContext.getPackageName() + "/";
    //call it so db get copied from assets to sdcard
    openToRead();
    close();
}

public void updateLaw(int lawID, String newSummary, String newFullText){

    int tagID = getTagID(lawID);
    String tagName = getTagName(tagID);
    int categoryID = getCategoryID(tagID);
    String categoryName = getCategoryName(categoryID);

    openToWrite();

        ContentValues contentValues = new ContentValues();
        contentValues.put(Constants.KEY_SUMMARY, newSummary);
        if(newFullText!=null)
            contentValues.put(Constants.KEY_FULL_TEXT, newFullText);

        mSqLiteDatabase.update(Constants.TABLE_LAW, contentValues, Constants.KEY_LAW_ID + "=" + lawID, null);
    close();
    try {
        insertNotification(categoryName, tagID + "/" + categoryID + " ~ " + categoryName + ", " + tagName + " has changed in " + getLocationName(getLocationID(lawID)));
    }
    catch(Exception e){
        exceptionHandler.alert(e, "UpdateLaw()");
    }

}

public int getCategoryID(int tagID){

    openToRead();

    int categoryID = 0;

    String Query = "SELECT * from " + Constants.TABLE_CATEGORY_TAG;
    Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
       if (cursor.moveToFirst()) {
            while (cursor.isAfterLast() == false) {
                    if (cursor.getInt(cursor.getColumnIndex(Constants.KEY_TAG_ID)) == tagID) {
                            int indexCategoryID = cursor.getColumnIndex(Constants.KEY_CATEGORY_ID);
                            categoryID = cursor.getInt(indexCategoryID);
                    }
                cursor.moveToNext();
            }
        }

    close();

    return categoryID;

}

public String getCategoryName(int categoryID){

    String categoryName = "";
    openToRead();

    String Query = "SELECT * from " + Constants.TABLE_CATEGORY;
    Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);

        if (cursor.moveToFirst()) {
            while (cursor.isAfterLast() == false) {
            if (cursor.getInt(cursor.getColumnIndex(Constants.KEY_CATEGORY_ID)) == categoryID) {
                        int indexCategoryName = cursor.getColumnIndex(Constants.KEY_CATEGORY_NAME);
                        categoryName = cursor.getString(indexCategoryName);
                }
                cursor.moveToNext();
            }
        }
    close();

    return categoryName.toLowerCase();

}

public int getLocationID(int lawID){
    openToRead();

    String Query = "SELECT * from " + Constants.TABLE_LAW_LOCATION;
    Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
    int locationID = 0;

    if (cursor.moveToFirst()) {
        while (cursor.isAfterLast() == false) {
            try {
                if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_LAW_ID)) == lawID) {
                    int indexTagID = cursor.getColumnIndex(Constants.KEY_LOCATION_ID);
                    locationID = cursor.getInt(indexTagID);
                }
            } catch (Exception e) {
                exceptionHandler.alert(e, "getLocationID()");
            }
            cursor.moveToNext();
        }
    }

    close();

    return locationID;

}

public String getLocationName(int locationID){
    openToRead();

    String Query = "SELECT * from " + Constants.TABLE_LOCATION;
    Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
    String locationName = "";

    if (cursor.moveToFirst()) {
        while (cursor.isAfterLast() == false) {
            try {
                if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_LOCATION_ID)) == locationID) {
                    int indexTagID = cursor.getColumnIndex(Constants.KEY_LOCATION_NAME);
                    locationName = cursor.getString(indexTagID);
                }
            } catch (Exception e) {
                exceptionHandler.alert(e, "getLocationName()");
            }
            cursor.moveToNext();
        }
    }

    close();

    return locationName;

}

public int getTagID(int lawID){

    openToRead();

    String Query = "SELECT * from " + Constants.TABLE_LAW_TAG;
    Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
    int tagID = 0;

    if (cursor.moveToFirst()) {
        while (cursor.isAfterLast() == false) {
            try {
                if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_LAW_ID)) == lawID) {
                    int indexTagID = cursor.getColumnIndex(Constants.KEY_TAG_ID);
                    tagID = cursor.getInt(indexTagID);
                }
            } catch (Exception e) {
                exceptionHandler.alert(e, "getTagID()");
            }
            cursor.moveToNext();
        }
    }

    close();

    return tagID;


}

public String getTagName(int tagID){

    openToRead();

    String tagName = "";

    String Query = "SELECT * from " + Constants.TABLE_TAG;
    Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);

    if(cursor.moveToFirst()){
        while (cursor.isAfterLast() == false) {
            try {
                if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_TAG_ID)) == tagID) {
                    int indexTagName = cursor.getColumnIndex(Constants.KEY_TAG_NAME);
                    tagName = cursor.getString(indexTagName);
                }
            } catch (Exception e) {
                exceptionHandler.alert(e, "getTagName()");
            }
            cursor.moveToNext();
        }
    }

    close();

    return tagName;
}

public void insertNotification(String type, String text){

    openToWrite();
    try {
        String tableQuery = "CREATE TABLE "+Constants.TABLE_NOTIFICATION+" (\n" +
            Constants.KEY_NOTIFICATION_ID +" INTEGER PRIMARY KEY AUTOINCREMENT," +
                Constants.KEY_LAW_ID + " INT,\n" +
                Constants.KEY_NOTIFICATION_TYPE + " VARCHAR,\n" +
                Constants.KEY_NOTIFICATION_TEXT + " VARCHAR,\n" +
            Constants.KEY_NOTIFICATION_TIME + " DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
            Constants.KEY_NOTIFICATION_STATUS + " VARCHAR\n" +
            ");\n";
        mSqLiteDatabase.execSQL(tableQuery);
    }

    catch(Exception e){
    }

    try {
        ContentValues contentValues = new ContentValues();
        contentValues.put(Constants.KEY_NOTIFICATION_TYPE, type);
        contentValues.put(Constants.KEY_NOTIFICATION_TEXT, text);
        contentValues.put(Constants.KEY_NOTIFICATION_STATUS, "unread");

        mSqLiteDatabase.insert(Constants.TABLE_NOTIFICATION, null, contentValues);
    }

    catch(Exception e){
        exceptionHandler.alert(e, "insertNotification()");
    }
    close();

}

public void updateNotificationStatus(int notificationID, String status){
    openToWrite();

    try {
        ContentValues contentValues = new ContentValues();
        contentValues.put(Constants.KEY_NOTIFICATION_STATUS, status);

        mSqLiteDatabase.update(Constants.TABLE_NOTIFICATION, contentValues, Constants.KEY_NOTIFICATION_ID + "=" + notificationID, null);
    }

    catch(Exception e){
        exceptionHandler.alert(e, "updateNotificationStatus()");
    }

    close();
}

public String getNotificationStatus(int notificationID){
    openToRead();

    String Query = "SELECT * FROM " + Constants.TABLE_NOTIFICATION + " WHERE " + Constants.KEY_NOTIFICATION_ID + "=" + notificationID;
    Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
    String notificationStatus = "";
    if(cursor.moveToFirst()){
        while (cursor.isAfterLast() == false) {
            try {
                if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_NOTIFICATION_ID)) == notificationID) {
                    int indexNotificationStatus = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_STATUS);
                    notificationStatus = cursor.getString(indexNotificationStatus);
                }
            } catch (Exception e) {
                exceptionHandler.alert(e, "getNotificationStatus()");
            }
            cursor.moveToNext();
        }
    }

    close();

    return notificationStatus;

}

public int getNotificationId(String time){

    openToRead();

    int notificationId = 0;
    try {
        String query = "SELECT * FROM " + Constants.TABLE_NOTIFICATION +
                " WHERE " + Constants.KEY_NOTIFICATION_TIME + " = " + time;
        Cursor cursor = mSqLiteDatabase.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            while (cursor.isAfterLast() == false) {
                int indexNotificationID = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_ID);
                notificationId = cursor.getInt(indexNotificationID);
            }
        }
    }
    catch(Exception e){
        exceptionHandler.alert(e, "getNotificationID()");
    }
    close();

    return notificationId;
}

public List<NotificationModel> getNotificationList(){
    List<NotificationModel> lstNotifications = new ArrayList<NotificationModel>();

    openToRead();

    String Query = "SELECT * from " + Constants.TABLE_NOTIFICATION;
    Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);

    if (cursor.moveToFirst()) {
        while (cursor.isAfterLast() == false) {
            try {
                int indexNotificationID = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_ID);
                int indexNotificationTime = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_TIME);
                int indexNotificationType = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_TYPE);
                int indexNotificationText = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_TEXT);
                int indexNotificationStatus = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_STATUS);

                String notificationID = cursor.getString(indexNotificationID);
                String notificationTime = cursor.getString(indexNotificationTime);
                String notificationType = cursor.getString(indexNotificationType);
                String notificationText = cursor.getString(indexNotificationText);
                String notificationStatus = cursor.getString(indexNotificationStatus);

                lstNotifications.add(0, new NotificationModel(notificationType, notificationText, notificationTime, notificationStatus));



            } catch (Exception e) {
                exceptionHandler.alert(e, "getNotificationList()");
            }
            cursor.moveToNext();
        }
    }

    close();

    return lstNotifications;
}

public void insertError(String deviceIdentifier){
    openToWrite();

    try {
        //if the exceptions table does not exist create it
        String query = "CREATE TABLE exceptions(" +
                         "ExceptionID INTEGER PRIMARY KEY, " +
                         "DeviceIdentifier VARCHAR, " +
                         "DatetimeStamp DATETIME DEFAULT CURRENT_TIMESTAMP" +
                       ");";
        mSqLiteDatabase.execSQL(query);
    }
    catch (Exception e){
    }

    try {
        //insert error into database
        String query = "INSERT INTO exceptions VALUES (null, '" + deviceIdentifier + "', null);";
        exceptionHandler.alert(new Exception(), query);
        mSqLiteDatabase.execSQL(query);
    }
    catch (Exception e){
        exceptionHandler.alert(e, "insertError()");
    }

    close();
}

public void showProgressDialog() {
    Activity activity = ((Activity) mContext);
    if (activity.isFinishing()) {
        return;
    }

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (null != mDialog && !mDialog.isShowing())
                mDialog.show();
        }
    });
}

public void hideProgressDialogue() {
    if (null != mDialog)
        mDialog.dismiss();
}

private SQLiteAdapter openToRead() throws android.database.SQLException {
    //TODO: need to change it from assets to server db
    sSqLiteHelper = new SQLiteHelper(mContext, Constants.DATABASE_NAME, null, DATABASE_VERSION);
    mSqLiteDatabase = sSqLiteHelper.getReadableDatabase();
    return this;
}

private SQLiteAdapter openToWrite() throws android.database.SQLException {
    sSqLiteHelper = new SQLiteHelper(mContext, Constants.DATABASE_NAME, null, DATABASE_VERSION);
    mSqLiteDatabase = sSqLiteHelper.getWritableDatabase();
    return this;
}

private void close() {
    sSqLiteHelper.close();
}

private void copyDataBase() throws IOException {

    //Open your local db as the input stream
    InputStream myInput = mContext.getAssets().open(Constants.DATABASE_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + Constants.DATABASE_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

}

Aucun commentaire:

Enregistrer un commentaire