Java Code Examples for android.database.Cursor#deactivate()

The following examples show how to use android.database.Cursor#deactivate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseCursorTest.java    From sqlite-android with Apache License 2.0 6 votes vote down vote up
@MediumTest
@Test
public void testRequeryWithSelection() {
    populateDefaultTable();

    Cursor c = mDatabase.rawQuery("SELECT data FROM test WHERE data = '" + sString1 + "'",
            null);
    assertNotNull(c);
    assertEquals(1, c.getCount());
    assertTrue(c.moveToFirst());
    assertEquals(sString1, c.getString(0));
    c.deactivate();
    c.requery();
    assertEquals(1, c.getCount());
    assertTrue(c.moveToFirst());
    assertEquals(sString1, c.getString(0));
    c.close();
}
 
Example 2
Source File: DatabaseCursorTest.java    From sqlite-android with Apache License 2.0 6 votes vote down vote up
@MediumTest
@Test
public void testRequeryWithSelectionArgs() {
    populateDefaultTable();

    Cursor c = mDatabase.rawQuery("SELECT data FROM test WHERE data = ?",
            new String[]{sString1});
    assertNotNull(c);
    assertEquals(1, c.getCount());
    assertTrue(c.moveToFirst());
    assertEquals(sString1, c.getString(0));
    c.deactivate();
    c.requery();
    assertEquals(1, c.getCount());
    assertTrue(c.moveToFirst());
    assertEquals(sString1, c.getString(0));
    c.close();
}
 
Example 3
Source File: ProcedureDAO.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Fetches the xml text content for a procedure
 *
 * @param context   the current Context
 * @param procedure the Uri of the Procedure which will be retrieved
 * @return the xml text content of the procedure
 */
public static String getXMLForProcedure(Context context, Uri procedure) {
    Cursor cursor = null;
    String procedureXml = "";
    try {
        cursor = context.getContentResolver().query(procedure,
                new String[]{Procedures.Contract.PROCEDURE},
                null, null, null);
        cursor.moveToFirst();
        procedureXml = cursor.getString(cursor.getColumnIndex(
                Procedures.Contract.PROCEDURE));
        cursor.deactivate();
    } catch (Exception e) {
        EventDAO.logException(context, e);
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return procedureXml;
}
 
Example 4
Source File: ConfigSystemDao.java    From YiBo with Apache License 2.0 6 votes vote down vote up
String get(SQLiteDatabase sqLiteDatabase,String name) {
	checkNull(name);
	StringBuilder sql = new StringBuilder();
	sql.append("SELECT Config_Value FROM ").append(TABLE);
	sql.append(" WHERE Config_Key = '").append(name).append("'");

	Cursor cursor = sqLiteDatabase.rawQuery(sql.toString(), null);
	String value = null;
	if (null != cursor ) {
		if(cursor.moveToFirst()){
			value = cursor.getString(cursor.getColumnIndex("Config_Value"));
		}
		cursor.deactivate();
		cursor.close();
	}
	return value;
}
 
Example 5
Source File: ConfigSystemDao.java    From YiBo with Apache License 2.0 6 votes vote down vote up
/**
 * 获取所有设置
 *
 * @return Hashtable 保存设置信息键值对的Hashtable
 */
public Hashtable<String, String> getConfigKeyValueMap() {
	Hashtable<String, String> settings = new Hashtable<String, String>();
	StringBuilder sql = new StringBuilder();
	sql.append("SELECT Config_Key, Config_Value From ").append(TABLE);

	SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
	Cursor cursor = sqLiteDatabase.rawQuery(sql.toString(), null);
	if (null != cursor) {
		if(cursor.moveToFirst()){
			do {
				settings.put(cursor.getString(cursor.getColumnIndex("Config_Key")), cursor.getString(cursor
						.getColumnIndex("Config_Value")));
			} while (cursor.isAfterLast());
		}
		cursor.deactivate();
		cursor.close();
	}
	return settings;
}
 
Example 6
Source File: BaseDao.java    From YiBo with Apache License 2.0 6 votes vote down vote up
List<T> find(SQLiteDatabase sqLiteDatabase, String sql) {
	if (Logger.isDebug()) {
		Log.d("SQLiteDatabase Query", sql);
	}
	List<T> resultList = null;
	Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
	if (cursor != null) {
		if (cursor.moveToFirst()) {
			resultList = new ArrayList<T>();
			do {
				resultList.add(extractData(sqLiteDatabase, cursor));
			} while (cursor.moveToNext());
		}
		cursor.deactivate();
		cursor.close();
		cursor = null;
	}
	return resultList;
}
 
Example 7
Source File: EditMicroBlogActivity.java    From YiBo with Apache License 2.0 6 votes vote down vote up
private void updateAttachedImage(Uri uri) {
	if (uri == null) {
		updateAttachedImage(null, 0);
		return;
	}

	String path = null;
	int rotate = 0;
	Cursor cursor = getContentResolver().query(uri, null, null, null, null);
	if (cursor != null) {
		int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		int rotateIndex = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
		if (cursor.moveToFirst()) {
			path = cursor.getString(columnIndex); //图片文件路径
			if (rotateIndex > 0) {
				rotate = cursor.getInt(rotateIndex);
			}
		}
		cursor.deactivate();
	    cursor.close();
	    updateAttachedImage(path, rotate);
	} else { //第三方文件管理器,不规范,直接返回原始路径
		path = getFilePath(uri);
		updateAttachedImage(path);
	}
}
 
Example 8
Source File: MergeCursor.java    From libcommon with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void deactivate() {
	synchronized (mCursors) {
		for (final Cursor cursor: mCursors) {
			if (cursor != null) {
				cursor.deactivate();
			}
		}
	}
	super.deactivate();
}
 
Example 9
Source File: DatabaseCursorTest.java    From sqlite-android with Apache License 2.0 5 votes vote down vote up
@MediumTest
@Test
public void testRequery() {
    populateDefaultTable();

    Cursor c = mDatabase.rawQuery("SELECT * FROM test", null);
    assertNotNull(c);
    assertEquals(3, c.getCount());
    c.deactivate();
    c.requery();
    assertEquals(3, c.getCount());
    c.close();
}
 
Example 10
Source File: DatabaseCursorTest.java    From sqlite-android with Apache License 2.0 5 votes vote down vote up
@MediumTest
@Test
public void testRequeryWithAlteredSelectionArgs() {
    // Test the ability of a subclass of SQLiteCursor to change its query arguments.
    populateDefaultTable();

    SQLiteDatabase.CursorFactory factory = new SQLiteDatabase.CursorFactory() {
        public Cursor newCursor(
            SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable,
            SQLiteQuery query) {
            return new SQLiteCursor(masterQuery, editTable, query) {
                @Override
                public boolean requery() {
                    setSelectionArguments(new String[]{"2"});
                    return super.requery();
                }
            };
        }
    };
    Cursor c = mDatabase.rawQueryWithFactory(
            factory, "SELECT data FROM test WHERE _id <= ?", new String[]{"1"},
            null);
    assertNotNull(c);
    assertEquals(1, c.getCount());
    assertTrue(c.moveToFirst());
    assertEquals(sString1, c.getString(0));

    // Our hacked requery() changes the query arguments in the cursor.
    c.requery();

    assertEquals(2, c.getCount());
    assertTrue(c.moveToFirst());
    assertEquals(sString1, c.getString(0));
    assertTrue(c.moveToNext());
    assertEquals(sString2, c.getString(0));

    // Test that setting query args on a deactivated cursor also works.
    c.deactivate();
    c.requery();
}
 
Example 11
Source File: BaseDao.java    From YiBo with Apache License 2.0 5 votes vote down vote up
T query(SQLiteDatabase sqLiteDatabase , String sql) {
	T t = null;
	Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
	if (cursor != null) {
		if (cursor.moveToFirst()) {
			t = extractData(sqLiteDatabase, cursor);
		}
		cursor.deactivate();
		cursor.close();
		cursor = null;
	}
	return t;
}