这是在android中为SqliteHelper做单例的正确方法吗?

时间:2016-04-08 07:28:04

标签: java android sqlite android-studio android-sqlite

我试图在android中为SqliteHelper写下正确的单例访问权限。从这里到那里得到了点点滴滴,形成最终版本,但不确定我是否在这里遗漏了一些东西。

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "EffiaSoft";
    private static final int DATABASE_VERSION = 1;
    private static SQLiteOpenHelper sInstance;
    private Semaphore semaphore = new Semaphore(1);
    private Context mCxt;

    /**
     * Constructor should be private to prevent direct instantiation.
     * make call to static method "getInstance()" instead.
     */
    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mCxt = context;
    }

    public static synchronized SQLiteOpenHelper getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new DatabaseHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    private static <T> void setFields(Cursor cursor, T returnObject, Field[] declaredFields, String[] cursorColumns) throws IllegalAccessException {
        for (Field declaredField : declaredFields) {
            String fieldName = declaredField.getName();
            boolean hasColumn = false;
            for (String columnName : cursorColumns) {
                if (columnName.equals(fieldName))
                    hasColumn = true;
                else
                    continue;
                break;
            }
            if (hasColumn) {
                declaredField.setAccessible(true);
                int columnIndex = cursor.getColumnIndex(fieldName);
                if (columnIndex >= 0) {
                    if (declaredField.getType() == Character.TYPE) {
                        String value = cursor.getString(columnIndex);
                        if (!ValidationHelper.isNullOrEmpty(value) && value.length() == 1) {
                            char c = value.charAt(0);
                            declaredField.set(returnObject, c);
                        }
                    } else if (declaredField.getType() == Short.TYPE) {
                        declaredField.set(returnObject, cursor.getShort(columnIndex));
                    } else if (declaredField.getType() == Integer.TYPE) {
                        declaredField.set(returnObject, cursor.getInt(columnIndex));
                    } else if (declaredField.getType() == Long.TYPE) {
                        declaredField.set(returnObject, cursor.getLong(columnIndex));
                    } else if (declaredField.getType() == Float.TYPE) {
                        declaredField.set(returnObject, cursor.getFloat(columnIndex));
                    } else if (declaredField.getType() == Double.TYPE) {
                        declaredField.set(returnObject, cursor.getDouble(columnIndex));
                    } else if (declaredField.getType() == Boolean.TYPE) {
                        String temp = cursor.getString(columnIndex);
                        declaredField.setBoolean(returnObject, temp.equalsIgnoreCase("Y"));
                    } else if (Date.class.equals(declaredField.getType())) {
                        if (!ValidationHelper.isNullOrEmpty(cursor.getString(columnIndex))) {
                            Date date = ValueFormatter.formatDateTime(cursor.getString(columnIndex));
                            declaredField.set(returnObject, date);
                        }
                    } else if (BigDecimal.class.equals(declaredField.getType())) {
                        declaredField.set(returnObject, BigDecimal.valueOf(cursor.getDouble(columnIndex)));
                    } else {
                        declaredField.set(returnObject, cursor.getString(columnIndex));
                    }
                }
            }

        }
    }

    private void Disconnect() {
        semaphore.release();
    }

    private void Connect() {
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            LogHelper.writeExceptionLog(e);
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    public <T> ArrayList<T> getData(Context context, String tableName, String columnName, String filterCondition, String orderBy,
                                    String groupBy, Class<T> type, SQLiteDatabase database) {

        Connect();
        ArrayList<T> arrayList = null;
        try {
            if (context != null && !ValidationHelper.isNullOrEmpty(tableName)) {
                SQLiteDatabase db;
                if (database == null) {
                    db = getWritableDatabase();
                } else
                    db = database;
                arrayList = new ArrayList<>();
                if (db != null) {
                    StringBuilder selectQuery = new StringBuilder();
                    selectQuery.append("SELECT ");
                    if (columnName != null && columnName.length() > 0)
                        selectQuery.append(columnName);
                    else
                        selectQuery.append("*");
                    selectQuery.append(" FROM ");
                    selectQuery.append(tableName);
                    if (!ValidationHelper.isNullOrEmpty(filterCondition)) {
                        selectQuery.append(" WHERE ");
                        selectQuery.append(filterCondition);
                    }
                    if (!ValidationHelper.isNullOrEmpty(orderBy)) {
                        selectQuery.append(" ORDER BY ");
                        selectQuery.append(orderBy);
                    }
                    if (!ValidationHelper.isNullOrEmpty(groupBy)) {
                        selectQuery.append(" GROUP BY ");
                        selectQuery.append(groupBy);
                    }
                    Cursor cursor;
                    cursor = db.rawQuery(selectQuery.toString(), null);
                    if (cursor != null) {
                        String[] cursorColumn = cursor.getColumnNames();
                        if (cursor.moveToFirst()) {
                            do {
                                T returnObject = type.newInstance();
                                Class<?> returnClass = returnObject.getClass();
                                if (!returnClass.getSuperclass().equals(Object.class)) {
                                    setFields(cursor, returnObject, returnClass.getSuperclass().getDeclaredFields(), cursorColumn);
                                }
                                setFields(cursor, returnObject, returnClass.getDeclaredFields(), cursorColumn);
                                arrayList.add(returnObject);
                            } while (cursor.moveToNext());
                        }

                    }
                }
            }
        } catch (Exception e) {
            LogHelper.writeExceptionLog(e);
        } finally {
            Disconnect();
        }
        return arrayList;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    @Override
    protected void finalize() throws Throwable {
        this.close();
        super.finalize();
        Disconnect();
    }
}

1 个答案:

答案 0 :(得分:0)

你只需要这样定义:

private static DatabaseHelper mInstance = null;

public static DatabaseHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new DatabaseHelper(context);
        }
        return mInstance;
   }

在您的活动中:

DatabaseHelper db = DatabaseHelper.getInstance(context);