Cursor.java revision 820e9b6b9cea6cce8115339dd774cdc273c4d6da
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.database; 18 19import android.content.ContentResolver; 20import android.net.Uri; 21import android.os.Bundle; 22 23/** 24 * This interface provides random read-write access to the result set returned 25 * by a database query. 26 * 27 * Cursor implementations are not required to be synchronized so code using a Cursor from multiple 28 * threads should perform its own synchronization when using the Cursor. 29 */ 30public interface Cursor { 31 /* 32 * Values returned by {@link #getType(int)}. 33 * These should be consistent with the corresponding types defined in CursorWindow.h 34 */ 35 /** Value returned by {@link #getType(int)} if the specified column is null */ 36 static final int FIELD_TYPE_NULL = 0; 37 38 /** Value returned by {@link #getType(int)} if the specified column type is integer */ 39 static final int FIELD_TYPE_INTEGER = 1; 40 41 /** Value returned by {@link #getType(int)} if the specified column type is float */ 42 static final int FIELD_TYPE_FLOAT = 2; 43 44 /** Value returned by {@link #getType(int)} if the specified column type is string */ 45 static final int FIELD_TYPE_STRING = 3; 46 47 /** Value returned by {@link #getType(int)} if the specified column type is blob */ 48 static final int FIELD_TYPE_BLOB = 4; 49 50 /** 51 * Returns the numbers of rows in the cursor. 52 * 53 * @return the number of rows in the cursor. 54 */ 55 int getCount(); 56 57 /** 58 * Returns the current position of the cursor in the row set. 59 * The value is zero-based. When the row set is first returned the cursor 60 * will be at positon -1, which is before the first row. After the 61 * last row is returned another call to next() will leave the cursor past 62 * the last entry, at a position of count(). 63 * 64 * @return the current cursor position. 65 */ 66 int getPosition(); 67 68 /** 69 * Move the cursor by a relative amount, forward or backward, from the 70 * current position. Positive offsets move forwards, negative offsets move 71 * backwards. If the final position is outside of the bounds of the result 72 * set then the resultant position will be pinned to -1 or count() depending 73 * on whether the value is off the front or end of the set, respectively. 74 * 75 * <p>This method will return true if the requested destination was 76 * reachable, otherwise, it returns false. For example, if the cursor is at 77 * currently on the second entry in the result set and move(-5) is called, 78 * the position will be pinned at -1, and false will be returned. 79 * 80 * @param offset the offset to be applied from the current position. 81 * @return whether the requested move fully succeeded. 82 */ 83 boolean move(int offset); 84 85 /** 86 * Move the cursor to an absolute position. The valid 87 * range of values is -1 <= position <= count. 88 * 89 * <p>This method will return true if the request destination was reachable, 90 * otherwise, it returns false. 91 * 92 * @param position the zero-based position to move to. 93 * @return whether the requested move fully succeeded. 94 */ 95 boolean moveToPosition(int position); 96 97 /** 98 * Move the cursor to the first row. 99 * 100 * <p>This method will return false if the cursor is empty. 101 * 102 * @return whether the move succeeded. 103 */ 104 boolean moveToFirst(); 105 106 /** 107 * Move the cursor to the last row. 108 * 109 * <p>This method will return false if the cursor is empty. 110 * 111 * @return whether the move succeeded. 112 */ 113 boolean moveToLast(); 114 115 /** 116 * Move the cursor to the next row. 117 * 118 * <p>This method will return false if the cursor is already past the 119 * last entry in the result set. 120 * 121 * @return whether the move succeeded. 122 */ 123 boolean moveToNext(); 124 125 /** 126 * Move the cursor to the previous row. 127 * 128 * <p>This method will return false if the cursor is already before the 129 * first entry in the result set. 130 * 131 * @return whether the move succeeded. 132 */ 133 boolean moveToPrevious(); 134 135 /** 136 * Returns whether the cursor is pointing to the first row. 137 * 138 * @return whether the cursor is pointing at the first entry. 139 */ 140 boolean isFirst(); 141 142 /** 143 * Returns whether the cursor is pointing to the last row. 144 * 145 * @return whether the cursor is pointing at the last entry. 146 */ 147 boolean isLast(); 148 149 /** 150 * Returns whether the cursor is pointing to the position before the first 151 * row. 152 * 153 * @return whether the cursor is before the first result. 154 */ 155 boolean isBeforeFirst(); 156 157 /** 158 * Returns whether the cursor is pointing to the position after the last 159 * row. 160 * 161 * @return whether the cursor is after the last result. 162 */ 163 boolean isAfterLast(); 164 165 /** 166 * Returns the zero-based index for the given column name, or -1 if the column doesn't exist. 167 * If you expect the column to exist use {@link #getColumnIndexOrThrow(String)} instead, which 168 * will make the error more clear. 169 * 170 * @param columnName the name of the target column. 171 * @return the zero-based column index for the given column name, or -1 if 172 * the column name does not exist. 173 * @see #getColumnIndexOrThrow(String) 174 */ 175 int getColumnIndex(String columnName); 176 177 /** 178 * Returns the zero-based index for the given column name, or throws 179 * {@link IllegalArgumentException} if the column doesn't exist. If you're not sure if 180 * a column will exist or not use {@link #getColumnIndex(String)} and check for -1, which 181 * is more efficient than catching the exceptions. 182 * 183 * @param columnName the name of the target column. 184 * @return the zero-based column index for the given column name 185 * @see #getColumnIndex(String) 186 * @throws IllegalArgumentException if the column does not exist 187 */ 188 int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException; 189 190 /** 191 * Returns the column name at the given zero-based column index. 192 * 193 * @param columnIndex the zero-based index of the target column. 194 * @return the column name for the given column index. 195 */ 196 String getColumnName(int columnIndex); 197 198 /** 199 * Returns a string array holding the names of all of the columns in the 200 * result set in the order in which they were listed in the result. 201 * 202 * @return the names of the columns returned in this query. 203 */ 204 String[] getColumnNames(); 205 206 /** 207 * Return total number of columns 208 * @return number of columns 209 */ 210 int getColumnCount(); 211 212 /** 213 * Returns the value of the requested column as a byte array. 214 * 215 * <p>If the native content of that column is not blob exception may throw 216 * 217 * @param columnIndex the zero-based index of the target column. 218 * @return the value of that column as a byte array. 219 */ 220 byte[] getBlob(int columnIndex); 221 222 /** 223 * Returns the value of the requested column as a String. 224 * 225 * <p>If the native content of that column is not text the result will be 226 * the result of passing the column value to String.valueOf(x). 227 * 228 * @param columnIndex the zero-based index of the target column. 229 * @return the value of that column as a String. 230 */ 231 String getString(int columnIndex); 232 233 /** 234 * Retrieves the requested column text and stores it in the buffer provided. 235 * If the buffer size is not sufficient, a new char buffer will be allocated 236 * and assigned to CharArrayBuffer.data 237 * @param columnIndex the zero-based index of the target column. 238 * if the target column is null, return buffer 239 * @param buffer the buffer to copy the text into. 240 */ 241 void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer); 242 243 /** 244 * Returns the value of the requested column as a short. 245 * 246 * <p>If the native content of that column is not numeric the result will be 247 * the result of passing the column value to Short.valueOf(x). 248 * 249 * @param columnIndex the zero-based index of the target column. 250 * @return the value of that column as a short. 251 */ 252 short getShort(int columnIndex); 253 254 /** 255 * Returns the value of the requested column as an int. 256 * 257 * <p>If the native content of that column is not numeric the result will be 258 * the result of passing the column value to Integer.valueOf(x). 259 * 260 * @param columnIndex the zero-based index of the target column. 261 * @return the value of that column as an int. 262 */ 263 int getInt(int columnIndex); 264 265 /** 266 * Returns the value of the requested column as a long. 267 * 268 * <p>If the native content of that column is not numeric the result will be 269 * the result of passing the column value to Long.valueOf(x). 270 * 271 * @param columnIndex the zero-based index of the target column. 272 * @return the value of that column as a long. 273 */ 274 long getLong(int columnIndex); 275 276 /** 277 * Returns the value of the requested column as a float. 278 * 279 * <p>If the native content of that column is not numeric the result will be 280 * the result of passing the column value to Float.valueOf(x). 281 * 282 * @param columnIndex the zero-based index of the target column. 283 * @return the value of that column as a float. 284 */ 285 float getFloat(int columnIndex); 286 287 /** 288 * Returns the value of the requested column as a double. 289 * 290 * <p>If the native content of that column is not numeric the result will be 291 * the result of passing the column value to Double.valueOf(x). 292 * 293 * @param columnIndex the zero-based index of the target column. 294 * @return the value of that column as a double. 295 */ 296 double getDouble(int columnIndex); 297 298 /** 299 * Returns data type of the given column's value. 300 * The preferred type of the column is returned but the data may be converted to other types 301 * as documented in the get-type methods such as {@link #getInt(int)}, {@link #getFloat(int)} 302 * etc. 303 *<p> 304 * Returned column types are 305 * <ul> 306 * <li>{@link #FIELD_TYPE_NULL}</li> 307 * <li>{@link #FIELD_TYPE_INTEGER}</li> 308 * <li>{@link #FIELD_TYPE_FLOAT}</li> 309 * <li>{@link #FIELD_TYPE_STRING}</li> 310 * <li>{@link #FIELD_TYPE_BLOB}</li> 311 *</ul> 312 *</p> 313 * 314 * @param columnIndex the zero-based index of the target column. 315 * @return column value type 316 */ 317 int getType(int columnIndex); 318 319 /** 320 * Returns <code>true</code> if the value in the indicated column is null. 321 * 322 * @param columnIndex the zero-based index of the target column. 323 * @return whether the column value is null. 324 */ 325 boolean isNull(int columnIndex); 326 327 /** 328 * Deactivates the Cursor, making all calls on it fail until {@link #requery} is called. 329 * Inactive Cursors use fewer resources than active Cursors. 330 * Calling {@link #requery} will make the cursor active again. 331 */ 332 void deactivate(); 333 334 /** 335 * Performs the query that created the cursor again, refreshing its 336 * contents. This may be done at any time, including after a call to {@link 337 * #deactivate}. 338 * 339 * Since this method could execute a query on the database and potentially take 340 * a while, it could cause ANR if it is called on Main (UI) thread. 341 * A warning is printed if this method is being executed on Main thread. 342 * 343 * @return true if the requery succeeded, false if not, in which case the 344 * cursor becomes invalid. 345 * @deprecated Don't use this. Just request a new cursor, so you can do this 346 * asynchronously and update your list view once the new cursor comes back. 347 */ 348 @Deprecated 349 boolean requery(); 350 351 /** 352 * Closes the Cursor, releasing all of its resources and making it completely invalid. 353 * Unlike {@link #deactivate()} a call to {@link #requery()} will not make the Cursor valid 354 * again. 355 */ 356 void close(); 357 358 /** 359 * return true if the cursor is closed 360 * @return true if the cursor is closed. 361 */ 362 boolean isClosed(); 363 364 /** 365 * Register an observer that is called when changes happen to the content backing this cursor. 366 * Typically the data set won't change until {@link #requery()} is called. 367 * 368 * @param observer the object that gets notified when the content backing the cursor changes. 369 * @see #unregisterContentObserver(ContentObserver) 370 */ 371 void registerContentObserver(ContentObserver observer); 372 373 /** 374 * Unregister an observer that has previously been registered with this 375 * cursor via {@link #registerContentObserver}. 376 * 377 * @param observer the object to unregister. 378 * @see #registerContentObserver(ContentObserver) 379 */ 380 void unregisterContentObserver(ContentObserver observer); 381 382 /** 383 * Register an observer that is called when changes happen to the contents 384 * of the this cursors data set, for example, when the data set is changed via 385 * {@link #requery()}, {@link #deactivate()}, or {@link #close()}. 386 * 387 * @param observer the object that gets notified when the cursors data set changes. 388 * @see #unregisterDataSetObserver(DataSetObserver) 389 */ 390 void registerDataSetObserver(DataSetObserver observer); 391 392 /** 393 * Unregister an observer that has previously been registered with this 394 * cursor via {@link #registerContentObserver}. 395 * 396 * @param observer the object to unregister. 397 * @see #registerDataSetObserver(DataSetObserver) 398 */ 399 void unregisterDataSetObserver(DataSetObserver observer); 400 401 /** 402 * Register to watch a content URI for changes. This can be the URI of a specific data row (for 403 * example, "content://my_provider_type/23"), or a a generic URI for a content type. 404 * 405 * @param cr The content resolver from the caller's context. The listener attached to 406 * this resolver will be notified. 407 * @param uri The content URI to watch. 408 */ 409 void setNotificationUri(ContentResolver cr, Uri uri); 410 411 /** 412 * onMove() will only be called across processes if this method returns true. 413 * @return whether all cursor movement should result in a call to onMove(). 414 */ 415 boolean getWantsAllOnMoveCalls(); 416 417 /** 418 * Returns a bundle of extra values. This is an optional way for cursors to provide out-of-band 419 * metadata to their users. One use of this is for reporting on the progress of network requests 420 * that are required to fetch data for the cursor. 421 * 422 * <p>These values may only change when requery is called. 423 * @return cursor-defined values, or Bundle.EMTPY if there are no values. Never null. 424 */ 425 Bundle getExtras(); 426 427 /** 428 * This is an out-of-band way for the the user of a cursor to communicate with the cursor. The 429 * structure of each bundle is entirely defined by the cursor. 430 * 431 * <p>One use of this is to tell a cursor that it should retry its network request after it 432 * reported an error. 433 * @param extras extra values, or Bundle.EMTPY. Never null. 434 * @return extra values, or Bundle.EMTPY. Never null. 435 */ 436 Bundle respond(Bundle extras); 437} 438