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

The following examples show how to use android.database.Cursor#isClosed() . 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: AbstractCursorLoader.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void deliverResult(Cursor newCursor) {
  if (isReset()) {
    if (newCursor != null) {
      newCursor.close();
    }
    return;
  }
  Cursor oldCursor = this.cursor;

  this.cursor = newCursor;

  if (isStarted()) {
    super.deliverResult(newCursor);
  }
  if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
    oldCursor.close();
  }
}
 
Example 2
Source File: CursorLoader.java    From sprinkles with Apache License 2.0 6 votes vote down vote up
@Override
public void deliverResult(Cursor cursor) {
	if (isReset()) {
		// An async query came in while the loader is stopped
		if (cursor != null) {
			cursor.close();
		}
		return;
	}
	Cursor oldCursor = mCursor;
	mCursor = cursor;

	if (isStarted()) {
		super.deliverResult(cursor);
	}

	if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
		oldCursor.close();
	}
}
 
Example 3
Source File: DownloadManager.java    From letv with Apache License 2.0 6 votes vote down vote up
public static DownloadVideo getDownloadVideoData(String vid) {
    Cursor cursor = null;
    DownloadVideo downloadVideo = null;
    try {
        Query query = new Query(sConext.getContentResolver());
        query.setUri(DOWNLOAD_VIDEO_URI);
        query.setSelection("vid = " + vid);
        cursor = query(query);
        cursor.moveToFirst();
        downloadVideo = DownloadVideoTable.cursorToDownloadVideo(cursor);
        if (!(cursor == null || cursor.isClosed())) {
            cursor.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (!(cursor == null || cursor.isClosed())) {
            cursor.close();
        }
    } catch (Throwable th) {
        if (!(cursor == null || cursor.isClosed())) {
            cursor.close();
        }
    }
    return downloadVideo;
}
 
Example 4
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/** 按条件查询对象并返回集合 */
public List<T> query(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
    long start = System.currentTimeMillis();
    lock.lock();
    List<T> list = new ArrayList<>();
    Cursor cursor = null;
    try {
        database.beginTransaction();
        cursor = database.query(getTableName(), columns, selection, selectionArgs, groupBy, having, orderBy, limit);
        while (!cursor.isClosed() && cursor.moveToNext()) {
            list.add(parseCursorToBean(cursor));
        }
        database.setTransactionSuccessful();
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        closeDatabase(null, cursor);
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " query");
    }
    return list;
}
 
Example 5
Source File: ChatDB.java    From TestChat with Apache License 2.0 6 votes vote down vote up
public List<String> getAllErrorMessage(boolean isUploadServerSuccess) {
        List<String> result=null;
        if (mDatabase.isOpen()) {
                String flag=isUploadServerSuccess?"1":"0";
                Cursor cursor=mDatabase.query(CRASH_TABLE,null,CRASH_FLAG+" =?",new String[]{flag},null,null,null);
                if (cursor != null) {
                        result=new ArrayList<>();
                        while (cursor.moveToNext()) {
                                result.add(cursor.getString(cursor.getColumnIndexOrThrow(CRASH_FLAG)));
                        }
                        if (!cursor.isClosed()) {
                                cursor.close();
                        }
                }
        }
        return result;
}
 
Example 6
Source File: DataHelper.java    From Field-Book with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the plot for items that match the specified id
 */
public String getPlotFromId(String plot_id) {
    try {
        Cursor cursor = db.query(RANGE, new String[]{TICK + ep.getString("ImportSecondName", "") + TICK},
                TICK + ep.getString("ImportUniqueName", "") + TICK + " like ?", new String[]{plot_id},
                null, null, null);

        String myList = null;

        if (cursor.moveToFirst()) {
            myList = cursor.getString(0);
        }

        if (!cursor.isClosed()) {
            cursor.close();
        }

        return myList;
    } catch (Exception e) {
        return null;
    }
}
 
Example 7
Source File: DataHelper.java    From Field-Book with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns saved data based on plot_id
 * v1.6 - Amended to consider both trait and format
 */
public HashMap getUserDetail(String plotId) {
    HashMap data = new HashMap();

    Cursor cursor = db.query(USER_TRAITS, new String[]{"parent", "trait",
                    "userValue", "rid"}, "rid like ?", new String[]{plotId},
            null, null, null
    );

    if (cursor.moveToFirst()) {
        do {
            data.put(cursor.getString(0), cursor.getString(2));
        } while (cursor.moveToNext());
    }

    if (!cursor.isClosed()) {
        cursor.close();
    }

    return data;
}
 
Example 8
Source File: AddIdCursorLoader.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
@Override
public void deliverResult(Cursor cursor) {
    if (isReset()) {
        // An async query came in while the loader is stopped
        if (cursor != null) {
            cursor.close();
        }
        return;
    }
    Cursor oldCursor = mCursor;
    mCursor = cursor;

    if (isStarted()) {
        super.deliverResult(cursor);
    }

    if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
        oldCursor.close();
    }
}
 
Example 9
Source File: DataHelper.java    From Field-Book with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the column names for the range table
 */
public String[] getRangeColumns() {
    Cursor cursor = db.rawQuery("SELECT * from range limit 1", null);

    String[] data = null;

    if (cursor.moveToFirst()) {
        int i = cursor.getColumnCount() - 1;

        data = new String[i];

        int k = 0;

        for (int j = 0; j < cursor.getColumnCount(); j++) {
            if (!cursor.getColumnName(j).equals("id")) {
                data[k] = cursor.getColumnName(j);
                k += 1;
            }
        }
    }

    if (!cursor.isClosed()) {
        cursor.close();
    }

    return data;
}
 
Example 10
Source File: ChatDB.java    From TestChat with Apache License 2.0 5 votes vote down vote up
/**
 * 通过用户ID和消息的创建时间判断最近消息是否存在
 *
 * @param uid  用户ID
 * @param time 消息时间
 * @return 结果
 */
private boolean isRecentMsgExist(String uid, String time) {
        boolean result = false;
        if (mDatabase.isOpen()) {
                Cursor cursor = mDatabase.rawQuery("select * from " + RECENT_TABLE_NAME + " where " + RECENT_TO_ID + " =? and " + RECENT_TO_TIME + " =?", new String[]{uid, time});
                if (cursor != null && cursor.moveToFirst()) {
                        result = true;
                }
                if (cursor != null && !cursor.isClosed()) {
                        cursor.close();
                }
        }
        return result;
}
 
Example 11
Source File: Download.java    From letv with Apache License 2.0 5 votes vote down vote up
public static DownloadAlbum cursorToDownloadAlbum(Cursor cursor) {
    boolean z = true;
    if (cursor == null || cursor.isClosed() || cursor.getCount() <= 0) {
        return null;
    }
    boolean z2;
    DownloadAlbum downloadAlbum = new DownloadAlbum();
    downloadAlbum.aid = cursor.getLong(cursor.getColumnIndex("aid"));
    downloadAlbum.picUrl = cursor.getString(cursor.getColumnIndex(DownloadBaseColumns.COLUMN_PIC));
    downloadAlbum.albumTitle = cursor.getString(cursor.getColumnIndex(COLUMN_ALBUMTITLE));
    downloadAlbum.albumTotalSize = cursor.getLong(cursor.getColumnIndex(COLUMN_ALBUMTOTALSIZE));
    downloadAlbum.albumVideoNum = cursor.getInt(cursor.getColumnIndex("albumVideoNum"));
    downloadAlbum.isWatch = cursor.getInt(cursor.getColumnIndex("isWatch")) == 1;
    downloadAlbum.timestamp = cursor.getLong(cursor.getColumnIndex("timestamp"));
    downloadAlbum.setAlbumVersion(cursor.getInt(cursor.getColumnIndex(COLUMN_ALBUM_VERSION)));
    if (cursor.getInt(cursor.getColumnIndex(COLUMN_ALBUM_VIDEONORAML)) == 1) {
        z2 = true;
    } else {
        z2 = false;
    }
    downloadAlbum.isVideoNormal = z2;
    if (cursor.getInt(cursor.getColumnIndex(COLUMN_ALBUM_FROM_RECOM)) != 1) {
        z = false;
    }
    downloadAlbum.isFrommRecom = z;
    return downloadAlbum;
}
 
Example 12
Source File: HttpCacheDaoImpl.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
@Override
public Map<String, HttpResponse> getHttpResponsesByType(int type) {
    StringBuilder whereClause = new StringBuilder();
    whereClause.append(DbConstants.HTTP_CACHE_TABLE_TYPE).append("=?");
    String[] whereClauseArgs = {Integer.toString(type)};

    synchronized (HttpCacheDaoImpl.class) {
        Cursor cursor = sqliteUtils.getDb().query(DbConstants.HTTP_CACHE_TABLE_TABLE_NAME, null,
                whereClause.toString(), whereClauseArgs, null, null, null);

        if (cursor == null) {
            return null;
        }

        Map<String, HttpResponse> httpResponseMap = new HashMap<String, HttpResponse>();
        if (cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                String url = cursor.getString(DbConstants.HTTP_CACHE_TABLE_URL_INDEX);
                if (StringUtils.isEmpty(url)) {
                    continue;
                }

                HttpResponse httpResponse = cursorToHttpResponse(cursor, url);
                if (httpResponse != null) {
                    httpResponseMap.put(url, httpResponse);
                }
            }
        }
        if (!cursor.isClosed()) {
            cursor.close();
        }
        return httpResponseMap;
    }
}
 
Example 13
Source File: CompositeCursorAdapter.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
public void removePartition(int partitionIndex) {
    Cursor cursor = mPartitions.get(partitionIndex).cursor;
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    mPartitions.remove(partitionIndex);
    invalidate();
    notifyDataSetChanged();
}
 
Example 14
Source File: DatabaseAdapter.java    From Smartlab with Apache License 2.0 4 votes vote down vote up
public Person getPersonById(Integer id) {
	Person person = null;

	SQLiteDatabase database = null;
	Cursor c = null;
	try {
		database = openHelper.getReadableDatabase();

		c = database.query("person", new String[] { "id", "name", "family",
				"birthdate" }, "id=?",
				new String[] { Integer.toString(id) }, null, null, null);

		if (c.moveToNext()) {
			person = new Person();

			Integer pId = c.getInt(c.getColumnIndex("id"));
			person.setId(pId);

			String name = c.getString(c.getColumnIndex("name"));
			person.setName(name);

			String family = c.getString(c.getColumnIndex("family"));
			person.setFamily(family);

			long birthdate = c.getLong(c.getColumnIndex("birthdate"));
			Date bDate = new Date(birthdate);
			person.setBirthdate(bDate);
		}

	} catch (Exception ex) {
		// TODO
	} finally {
		if (c != null && !c.isClosed()) {
			c.close();
		}
		if (database != null) {
			database.close();
		}
	}

	return person;
}
 
Example 15
Source File: DownloadFileInfo.java    From file-downloader with Apache License 2.0 4 votes vote down vote up
/**
 * constructor of HttpDownloader, use {@link Cursor} to create
 *
 * @param cursor database cursor
 */
DownloadFileInfo(Cursor cursor) {
    if (cursor != null && !cursor.isClosed()) {
        int id = -1;
        String url = null;
        long downloadedSize = 0;
        long fileSize = 0;
        String eTag = null;
        String lastModified = null;
        String acceptRangeType = null;
        String fileDir = null;
        String tempFileName = null;
        String fileName = null;
        int status = Status.DOWNLOAD_STATUS_UNKNOWN;
        String createDatetime = null;

        int columnIndex = -1;
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_ID);
        if (columnIndex != -1) {
            id = cursor.getInt(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_URL);
        if (columnIndex != -1) {
            url = cursor.getString(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_DOWNLOADED_SIZE);
        if (columnIndex != -1) {
            downloadedSize = cursor.getLong(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_FILE_SIZE);
        if (columnIndex != -1) {
            fileSize = cursor.getLong(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_E_TAG);
        if (columnIndex != -1) {
            eTag = cursor.getString(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_LAST_MODIFIED);
        if (columnIndex != -1) {
            lastModified = cursor.getString(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_ACCEPT_RANGE_TYPE);
        if (columnIndex != -1) {
            acceptRangeType = cursor.getString(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_FILE_DIR);
        if (columnIndex != -1) {
            fileDir = cursor.getString(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_TEMP_FILE_NAME);
        if (columnIndex != -1) {
            tempFileName = cursor.getString(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_FILE_NAME);
        if (columnIndex != -1) {
            fileName = cursor.getString(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_STATUS);
        if (columnIndex != -1) {
            status = cursor.getInt(columnIndex);
        }
        columnIndex = cursor.getColumnIndex(Table.COLUMN_NAME_OF_FIELD_CREATE_DATETIME);
        if (columnIndex != -1) {
            createDatetime = cursor.getString(columnIndex);
        }
        if (id > 0 && !TextUtils.isEmpty(url)) {
            // init fields
            this.mId = id;
            this.mUrl = url;
            this.mDownloadedSize = downloadedSize;
            this.mFileSize = fileSize;
            this.mETag = eTag;
            this.mLastModified = lastModified;
            this.mAcceptRangeType = acceptRangeType;
            this.mFileDir = fileDir;
            this.mTempFileName = tempFileName;
            this.mFileName = fileName;
            this.mStatus = status;
            this.mCreateDatetime = createDatetime;
        } else {
            throw new IllegalArgumentException("id or url from cursor illegal!");
        }
    } else {
        throw new NullPointerException("cursor illegal!");
    }
}
 
Example 16
Source File: NotificationBadge.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public static void close(Cursor cursor) {
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
}
 
Example 17
Source File: RawSqlCursorLoader.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCanceled(Cursor cursor) {
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
}
 
Example 18
Source File: MainActivity.java    From WiFiAfterConnect with Apache License 2.0 4 votes vote down vote up
@Override
public void onCanceled(Cursor cursor) {
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
}
 
Example 19
Source File: CursorHelper.java    From libcommon with Apache License 2.0 4 votes vote down vote up
/**
 * 指定したCursorの現在のレコードを文字列に変換
 * @param cursor
 */
public static String toString(@Nullable final Cursor cursor) {
	if (cursor == null) {
		return "{null}";
	} else if (cursor.isClosed()) {
		return "{closed}";
	} else if (cursor.isBeforeFirst()) {
		return "{before first}";
	} else if (cursor.isAfterLast()) {
		return "{after last}";
	} else {
		final StringBuilder sb = new StringBuilder();
		final int n = cursor.getColumnCount();
		final String[] columnNames = cursor.getColumnNames();
		sb.append("{");
		for (int i = 0; i < n; i++) {
			switch (cursor.getType(i)) {
			case Cursor.FIELD_TYPE_FLOAT:
				sb.append(columnNames[i]).append("=").append(cursor.getDouble(i));
				break;
			case Cursor.FIELD_TYPE_INTEGER:
				sb.append(columnNames[i]).append("=").append(cursor.getLong(i));
				break;
			case Cursor.FIELD_TYPE_STRING:
				sb.append(columnNames[i]).append("=").append(cursor.getString(i));
				break;
			case Cursor.FIELD_TYPE_BLOB:
				sb.append(columnNames[i]).append("=").append("BLOB");
				break;
			case Cursor.FIELD_TYPE_NULL:
				sb.append(columnNames[i]).append("=").append("NULL");
				break;
			default:
				sb.append(columnNames[i]).append("=").append("UNKNOWN");
				break;
			}
			if (i < n-1) {
				sb.append(",");
			}
		}
		sb.append("}");
		return sb.toString();
	}
}
 
Example 20
Source File: SQLiteSync.java    From SQLite-sync.com with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Clear updated & deleted records marker
 */
private void clearChangesMarker(){
    String query;
    SQLiteDatabase db = null;
    Cursor cursor = null;

    try{
        db = openOrCreateDatabase(_dbFileName, null);

        List<String> tables = new ArrayList<String>();
        query = "select tbl_Name from sqlite_master where type='table' and sql like '%RowId%';";
        cursor = db.rawQuery(query, null);
        while (cursor.moveToNext()){
            tables.add(cursor.getString(0));
        }
        cursor.close();

        db.beginTransaction();
        for(String tableName : tables){
            if(tableName.equalsIgnoreCase("MergeIdentity")) {
                db.execSQL(String.format("update MergeIdentity set MergeUpdate=0 where MergeUpdate > 0;"));
            }

            if(!tableName.equalsIgnoreCase("MergeDelete") && !tableName.equalsIgnoreCase("MergeIdentity")) {
                String updTriggerSQL = null;

                query = String.format("select sql from sqlite_master where type='trigger' and name like 'trMergeUpdate_%1$s'", tableName);
                cursor = db.rawQuery(query, null);
                if(cursor.moveToFirst()){
                    updTriggerSQL = cursor.getString(0);
                }
                cursor.close();

                if(updTriggerSQL != null){
                    db.execSQL(String.format("drop trigger trMergeUpdate_%1$s;", tableName));
                    db.execSQL(String.format("update %1$s set MergeUpdate=0 where MergeUpdate > 0;", tableName));
                    db.execSQL(updTriggerSQL);
                }
            }
        }

        db.execSQL("delete from MergeDelete");
        db.setTransactionSuccessful();
    }
    finally {
        if(cursor != null && !cursor.isClosed()){
            cursor.close();
        }
        if(db != null && db.isOpen()){
            if(db.inTransaction()){
                db.endTransaction();
            }
            db.close();
        }
    }
}