Example usage for android.database Cursor moveToNext

List of usage examples for android.database Cursor moveToNext

Introduction

In this page you can find the example usage for android.database Cursor moveToNext.

Prototype

boolean moveToNext();

Source Link

Document

Move the cursor to the next row.

Usage

From source file:android.database.DatabaseUtils.java

/**
 * Prints the contents of a Cursor to a PrintSteam. The position is restored
 * after printing.//from  ww w  . j  a  v  a2  s .c o  m
 *
 * @param cursor the cursor to print
 * @param stream the stream to print to
 */
public static void dumpCursor(Cursor cursor, PrintStream stream) {
    stream.println(">>>>> Dumping cursor " + cursor);
    if (cursor != null) {
        int startPos = cursor.getPosition();

        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
            dumpCurrentRow(cursor, stream);
        }
        cursor.moveToPosition(startPos);
    }
    stream.println("<<<<<");
}

From source file:android.database.DatabaseUtils.java

/**
 * Prints the contents of a Cursor to a StringBuilder. The position
 * is restored after printing.//from  w  w w  .  ja  va  2s . c  o m
 *
 * @param cursor the cursor to print
 * @param sb the StringBuilder to print to
 */
public static void dumpCursor(Cursor cursor, StringBuilder sb) {
    sb.append(">>>>> Dumping cursor " + cursor + "\n");
    if (cursor != null) {
        int startPos = cursor.getPosition();

        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
            dumpCurrentRow(cursor, sb);
        }
        cursor.moveToPosition(startPos);
    }
    sb.append("<<<<<\n");
}

From source file:net.eledge.android.toolkit.db.internal.TableBuilder.java

private List<String> getExistingFields(Class<?> clazz) {
    List<String> names = new ArrayList<>();
    StringBuilder sb = new StringBuilder("pragma table_info(");
    sb.append(SQLBuilder.getTableName(clazz));
    sb.append(");");
    Cursor cursor = db.rawQuery(sb.toString(), new String[] {});
    if ((cursor != null) && cursor.moveToFirst()) {
        do {/*  www  . j  av a 2s  .c o  m*/
            names.add(cursor.getString(1));
        } while (cursor.moveToNext());
        cursor.close();
    }
    return names;
}

From source file:com.manning.androidhacks.hack023.dao.TodoDAO.java

public Todo isTodoInDb(ContentResolver contentResolver, Long serverId) {
    Todo todo = null;/*  ww w.j av  a2 s  .  c  om*/

    Cursor cursor = contentResolver.query(TodoContentProvider.CONTENT_URI, null,
            TodoContentProvider.COLUMN_SERVER_ID + "=" + serverId, null, null);

    if (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_TITLE));
        int status = cursor.getInt(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_STATUS_FLAG));

        todo = new Todo();
        todo.setId(serverId);
        todo.setTitle(name);
        todo.setStatus(status);
    }

    cursor.close();
    return todo;
}

From source file:com.manning.androidhacks.hack023.dao.TodoDAO.java

private List<Todo> getTodosWithSelection(ContentResolver contentResolver, String selection) {
    Cursor cursor = contentResolver.query(TodoContentProvider.CONTENT_URI, null, selection, null, null);

    List<Todo> list = new ArrayList<Todo>();

    while (cursor.moveToNext()) {
        int localId = cursor.getInt(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_ID));
        int serverId = cursor.getInt(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_SERVER_ID));
        String name = cursor.getString(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_TITLE));
        int status = cursor.getInt(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_STATUS_FLAG));

        Todo currentTodo = new Todo();

        if (status == StatusFlag.ADD) {
            currentTodo.setId((long) localId);
        } else {//from w  w  w.  j  a v  a2s .  c om
            currentTodo.setId((long) serverId);
        }
        currentTodo.setTitle(name);
        currentTodo.setStatus(status);

        list.add(currentTodo);
    }

    cursor.close();
    return list;
}

From source file:cn.newgxu.android.notty.service.FetchService.java

@Override
protected void onHandleIntent(Intent intent) {
    //      int method = intent.getIntExtra("method", RESTMethod.GET);
    //      switch (method) {
    //      case RESTMethod.GET:
    //         break;
    //      case RESTMethod.POST:
    //         Map<String, Object> params = new HashMap<String, Object>();
    //         params.put(C.notice.TITLE, intent.getStringExtra(C.notice.TITLE));
    //         params.put(C.notice.CONTENT, intent.getStringExtra(C.notice.CONTENT));
    //         L.d(TAG, "post result: %s", result);
    //         ContentValues values = new ContentValues();
    //         values.put(C.notice.TITLE, intent.getStringExtra(C.notice.TITLE));
    //         values.put(C.notice.CONTENT, intent.getStringExtra(C.notice.CONTENT));
    //         getContentResolver().insert(Uri.parse(C.BASE_URI + C.NOTICES), values);
    //         return;
    //      default:
    //         break;
    //      }/*from   w  w w.java 2  s  .c o  m*/
    if (NetworkUtils.connected(getApplicationContext())) {
        Cursor c = getContentResolver().query(Uri.parse(C.BASE_URI + C.NOTICES), C.notice.LATEST_NOTICE_ID,
                null, null, null);
        String uri = intent.getStringExtra(C.URI);
        if (c.moveToNext()) {
            uri += "&local_nid=" + c.getLong(0);
        }
        Log.d(TAG, String.format("uri: %s", uri));
        JSONObject result = RESTMethod.get(uri);
        L.d(TAG, "json: %s", result);
        try {
            JSONArray notices = result.getJSONArray(C.NOTICES);
            ContentValues[] noticeValues = new ContentValues[notices.length()];
            ContentValues[] userValues = new ContentValues[notices.length()];
            for (int i = 0; i < userValues.length; i++) {
                JSONObject n = notices.getJSONObject(i);
                JSONObject u = n.getJSONObject(C.USER);
                noticeValues[i] = Processor.json2Notice(n);
                userValues[i] = Processor.json2User(u);
            }
            String[] uris = intent.getStringArrayExtra("uris");
            getContentResolver().bulkInsert(Uri.parse(uris[0]), noticeValues);
            getContentResolver().bulkInsert(Uri.parse(uris[1]), userValues);

            final int newerCount = notices.length();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), getString(R.string.newer_count, newerCount),
                            Toast.LENGTH_SHORT).show();
                }
            });
        } catch (JSONException e) {
            throw new RuntimeException("ERROR when resolving json -> " + result, e);
        }
    } else {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), R.string.network_down, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

From source file:com.android.unit_tests.CheckinProviderTest.java

@MediumTest
public void testEventReport() {
    long start = System.currentTimeMillis();
    ContentResolver r = getContext().getContentResolver();
    Checkin.logEvent(r, Checkin.Events.Tag.TEST, "Test Value");

    Cursor c = r.query(Checkin.Events.CONTENT_URI, null, Checkin.Events.TAG + "=?",
            new String[] { Checkin.Events.Tag.TEST.toString() }, null);

    long id = -1;
    while (c.moveToNext()) {
        String tag = c.getString(c.getColumnIndex(Checkin.Events.TAG));
        String value = c.getString(c.getColumnIndex(Checkin.Events.VALUE));
        long date = c.getLong(c.getColumnIndex(Checkin.Events.DATE));
        assertEquals(Checkin.Events.Tag.TEST.toString(), tag);
        if ("Test Value".equals(value) && date >= start) {
            assertTrue(id < 0);//from w w w  .j  a v a 2  s  .  c  om
            id = c.getInt(c.getColumnIndex(Checkin.Events._ID));
        }
    }
    assertTrue(id > 0);

    int rows = r.delete(ContentUris.withAppendedId(Checkin.Events.CONTENT_URI, id), null, null);
    assertEquals(1, rows);
    c.requery();
    while (c.moveToNext()) {
        long date = c.getLong(c.getColumnIndex(Checkin.Events.DATE));
        assertTrue(date < start); // Have deleted the only newer TEST.
    }

    c.close();
}

From source file:ru.gkpromtech.exhibition.db.DbHelper.java

public String getPragma(SQLiteDatabase db, String pragma) {
    String value = null;//w  w w . jav  a  2  s.c o m
    Cursor cursor = db.rawQuery("PRAGMA " + pragma, null);
    if (cursor.moveToNext())
        value = cursor.getString(0);
    cursor.close();
    return value;
}

From source file:com.ruuhkis.cookies.CookieSQLSource.java

/**
 * /*from  w  ww. j  av a  2  s.co  m*/
 * @return a list of all cookies stored in cookies table
 */

public List<SQLCookie> getCookies() {
    List<SQLCookie> cookies = new ArrayList<SQLCookie>();
    Cursor cursor = db.query(CookieSQLHelper.COOKIE_TABLE_NAME, null, null, null, null, null, null);
    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        cookies.add(cursorToCookie(cursor));
        cursor.moveToNext();
    }

    cursor.close();

    return cookies;
}

From source file:com.granita.contacticloudsync.resource.LocalCalendar.java

@Override
@SuppressWarnings("Recycle")
public String getCTag() throws CalendarStorageException {
    try {/* w w w.j a va 2 s .com*/
        @Cleanup
        Cursor cursor = provider.query(calendarSyncURI(), new String[] { COLUMN_CTAG }, null, null, null);
        if (cursor != null && cursor.moveToNext())
            return cursor.getString(0);
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't read local (last known) CTag", e);
    }
    return null;
}