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:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void expireReadMessages(Context context, boolean expireAll, long expireTime) {

    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbwrite = db.getWritableDatabase();

    // Get all the expired messages so we can delete bodies and attachments
    long currentTime = System.currentTimeMillis();
    String q = null;/*  w  w w .  ja v a 2s.  c  o  m*/

    if (expireAll) {
        q = "SELECT _id, subscribed_group_id, has_attachments, attachments_fnames " + "FROM headers "
                + "WHERE read=1 AND catched=1";
    } else {
        q = "SELECT _id, subscribed_group_id, has_attachments, attachments_fnames " + "FROM headers "
                + "WHERE read=1 AND catched=1 AND read_unixdate < " + currentTime + " - " + expireTime;
    }

    Cursor c = dbwrite.rawQuery(q, null);

    int count = c.getCount();
    c.moveToFirst();
    String groupname;

    for (int i = 0; i < count; i++) {

        groupname = getGroupNameFromId(c.getInt(1) /*subscribed_group_id*/, context);
        FSUtils.deleteCacheMessage(c.getInt(0)/* _id */, groupname);

        if (c.getInt(2)/*has_attach*/ == 1) {
            FSUtils.deleteAttachments(c.getString(3) /*attachments_fnames*/, groupname);
        }

        c.moveToNext();
    }

    if (expireAll)
        q = "DELETE FROM headers WHERE read=1";
    else
        q = "DELETE FROM headers WHERE read=1 AND read_unixdate < " + currentTime + " - " + expireTime;
    dbwrite.execSQL(q);
    c.close();
    dbwrite.close();
    db.close();
}

From source file:com.hichinaschool.flashcards.libanki.Tags.java

/** Add any missing tags from notes to the tags list. */
public void registerNotes(long[] nids) {
    // when called without an argument, the old list is cleared first.
    String lim;// w w  w .  j  av a2s .co  m
    if (nids != null) {
        lim = " WHERE id IN " + Utils.ids2str(nids);
    } else {
        lim = "";
        mTags.clear();
        mChanged = true;
    }
    ArrayList<String> tags = new ArrayList<String>();
    Cursor cursor = null;
    try {
        cursor = mCol.getDb().getDatabase().rawQuery("SELECT DISTINCT tags FROM notes", null);
        while (cursor.moveToNext()) {
            for (String t : cursor.getString(0).split("\\s")) {
                if (t.length() > 0) {
                    tags.add(t);
                }
            }
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    register(tags);
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static Set<Integer> getChildrenIds(String resourcePath) {
    Set<Integer> ids = new TreeSet<Integer>();
    final String[] projection = new String[] { Nodes._ID, Nodes.NODE_RESOURCE_STATE };
    final String selection = Nodes.NODE_PARENT_PATH + "=?";
    //+ " AND " + Nodes.NODE_RESOURCE_STATE + " IS NULL"; // FIXME
    final String[] selectionArgs = new String[] { resourcePath };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    try {//from w  ww. j a v  a2s  . c  o m
        if (c.moveToFirst()) {
            int id;
            do {
                id = c.getInt(c.getColumnIndex(Nodes._ID));
                // We check the state, above SQL is failing to filter out
                // nodes which are in non-null state. No idea why.
                String s = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_STATE));
                if (s == null) {
                    ids.add(id);
                } else {
                    Log.d("MetaUtilities", "child state != null, ignoring");
                }
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return ids;
}

From source file:com.hichinaschool.flashcards.libanki.Note.java

public ArrayList<Card> cards() {
    ArrayList<Card> cards = new ArrayList<Card>();
    Cursor cur = null;
    try {//from ww  w.  ja  va  2  s .c  o m
        cur = mCol.getDb().getDatabase().rawQuery("SELECT id FROM cards WHERE nid = " + mId + " ORDER BY ord",
                null);
        while (cur.moveToNext()) {
            cards.add(mCol.getCard(cur.getLong(0)));
        }
    } finally {
        if (cur != null) {
            cur.close();
        }
    }
    return cards;
}

From source file:com.prey.json.actions.ContactsBackup.java

@TargetApi(Build.VERSION_CODES.ECLAIR)
public HashMap<String, String> contacts2(Context ctx) {
    HashMap<String, String> parametersMap = new HashMap<String, String>();
    String phoneNumber = null;/*from  w w  w .  java2  s.  c  o  m*/
    int phoneType;

    String email = null;
    int emailType;

    Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

    String _ID = ContactsContract.Contacts._ID;

    String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;

    String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

    Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;

    String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
    String NUMBER_TYPE = ContactsContract.CommonDataKinds.Phone.TYPE;

    Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;

    String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;

    String DATA = ContactsContract.CommonDataKinds.Email.DATA;
    String DATATYPE = ContactsContract.CommonDataKinds.Email.TYPE;

    Cursor cursor = ctx.getContentResolver().query(CONTENT_URI, null, null, null, null);

    // Loop for every contact in the phone

    if (cursor.getCount() > 0) {

        int i = 0;
        while (cursor.moveToNext()) {

            String contact_id = cursor.getString(cursor.getColumnIndex(_ID));

            String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));

            int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));

            if (hasPhoneNumber > 0) {

                PreyLogger.i("____________");
                parametersMap.put("contacts_backup[" + i + "][display_name]", name);
                parametersMap.put("contacts_backup[" + i + "][nickname][name]", name);
                parametersMap.put("contacts_backup[" + i + "][nickname][label]", name);

                PreyLogger.i("contacts_backup[" + i + "][display_name]" + name);
                PreyLogger.i("contacts_backup[" + i + "][nickname][name]" + name);
                PreyLogger.i("contacts_backup[" + i + "][nickname][label]" + name);

                // Query and loop for every phone number of the contact

                Cursor phoneCursor = ctx.getContentResolver().query(PhoneCONTENT_URI, null,
                        Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);

                int j = 0;
                while (phoneCursor.moveToNext()) {

                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    phoneType = phoneCursor.getInt(phoneCursor.getColumnIndex(NUMBER_TYPE));

                    parametersMap.put("contacts_backup[" + i + "][phones][" + j + "][number]", phoneNumber);
                    parametersMap.put("contacts_backup[" + i + "][phones][" + j + "][type]",
                            typePhone(phoneType));

                    PreyLogger.i("contacts_backup[" + i + "][phones][" + j + "][number]" + phoneNumber);
                    PreyLogger.i("contacts_backup[" + i + "][phones][" + j + "][type]" + typePhone(phoneType));

                    j++;

                } // while phone

                phoneCursor.close();

                // Query and loop for every email of the contact

                Cursor emailCursor = ctx.getContentResolver().query(EmailCONTENT_URI, null,
                        EmailCONTACT_ID + " = ?", new String[] { contact_id }, null);

                j = 0;
                while (emailCursor.moveToNext()) {

                    email = emailCursor.getString(emailCursor.getColumnIndex(DATA));
                    emailType = emailCursor.getInt(emailCursor.getColumnIndex(DATATYPE));
                    parametersMap.put("contacts_backup[" + i + "][emails][" + j + "][address]", email);
                    parametersMap.put("contacts_backup[" + i + "][emails][" + j + "][type]",
                            typeMail(emailType));

                    PreyLogger.i("contacts_backup[" + i + "][emails][" + j + "][address]" + email);
                    PreyLogger.i("contacts_backup[" + i + "][emails][" + j + "][type]" + typeMail(emailType));
                    j++;
                } // while email

                emailCursor.close();
                i = i + 1;
            } // if hasPhoneNumber

        } // while cursor
          // 
    } // if cursor
    return parametersMap;

}

From source file:net.mEmoZz.PopMovies.frags.MoviesFragment.java

public List<Movie> getOfflineData() {
    List<Movie> movies = new ArrayList<>();
    Cursor c = helper.getData();
    if (c != null) {
        while (c.moveToNext()) {
            String movId = c.getString(1);
            String movPoster = c.getString(2);
            String movBackdrop = c.getString(3);
            String movName = c.getString(4);
            String movDate = c.getString(5);
            String movVote = c.getString(6);
            String movOverview = c.getString(7);

            Movie movie = new Movie();
            movie.setId(movId);/* w ww  . j a v  a2 s .c om*/
            movie.setPoster(movPoster);
            movie.setBackdrop(movBackdrop);
            movie.setTitle(movName);
            movie.setDate(movDate);
            movie.setVoteAvr(movVote);
            movie.setOverview(movOverview);
            movies.add(movie);
        }
        return movies;
    } else {
        return null;
    }
}

From source file:com.googlecode.android_scripting.facade.ContactsFacade.java

@Rpc(description = "Returns a List of all contacts.", returns = "a List of contacts as Maps")
public List<JSONObject> contactsGet(@RpcParameter(name = "attributes") @RpcOptional JSONArray attributes)
        throws JSONException {
    List<JSONObject> result = new ArrayList<JSONObject>();
    String[] columns;/*from w  ww.j  av a 2 s. c  o m*/
    if (attributes == null || attributes.length() == 0) {
        // In case no attributes are specified we set the default ones.
        columns = new String[] { "_id", "name", "primary_phone", "primary_email", "type" };
    } else {
        // Convert selected attributes list into usable string list.
        columns = new String[attributes.length()];
        for (int i = 0; i < attributes.length(); i++) {
            columns[i] = attributes.getString(i);
        }
    }
    List<String> queryList = new ArrayList<String>();
    for (String s : columns) {
        queryList.add(s);
    }
    if (!queryList.contains("_id")) {
        queryList.add("_id");
    }

    String[] query = queryList.toArray(new String[queryList.size()]);
    Cursor cursor = mContentResolver.query(CONTACTS_URI, query, null, null, null);
    if (cursor != null) {
        int idIndex = cursor.getColumnIndex("_id");
        while (cursor.moveToNext()) {
            String id = cursor.getString(idIndex);
            JSONObject message = new JSONObject();
            for (int i = 0; i < columns.length; i++) {
                String key = columns[i];
                String value = cursor.getString(cursor.getColumnIndex(key));
                if (mPhoneNumber != null) {
                    if (key.equals("primary_phone")) {
                        value = findPhone(id);
                    }
                }
                message.put(key, value);
            }
            result.add(message);
        }
        cursor.close();
    }
    return result;
}

From source file:github.popeen.dsub.util.SongDBHandler.java

public JSONArray exportData() {
    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns = { SONGS_ID, SONGS_SERVER_KEY, SONGS_SERVER_ID, SONGS_COMPLETE_PATH, SONGS_LAST_PLAYED,
            SONGS_LAST_COMPLETED };/*from  w w w .jav  a 2 s .co  m*/
    Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_LAST_PLAYED + " != ''", null, null, null, null, null);
    try {
        JSONArray jsonSongDb = new JSONArray();
        while (cursor.moveToNext()) {
            JSONObject tempJson = new JSONObject();
            tempJson.put("SONGS_ID", cursor.getInt(0));
            tempJson.put("SONGS_SERVER_KEY", cursor.getInt(1));
            tempJson.put("SONGS_SERVER_ID", cursor.getString(2));
            tempJson.put("SONGS_COMPLETE_PATH", cursor.getString(3));
            tempJson.put("SONGS_LAST_PLAYED", cursor.getInt(4));
            tempJson.put("SONGS_LAST_COMPLETED", cursor.getInt(5));
            jsonSongDb.put(tempJson);
        }
        cursor.close();
        return jsonSongDb;
    } catch (Exception e) {
    }
    return new JSONArray();
}

From source file:com.gimranov.zandy.app.task.APIRequest.java

/**
 * Returns APIRequest objects from the database
 * @return/*  w ww .j av a  2  s .c  om*/
 */
public static ArrayList<APIRequest> queue(Database db) {
    ArrayList<APIRequest> list = new ArrayList<APIRequest>();
    String[] cols = Database.REQUESTCOLS;
    String[] args = {};

    Cursor cur = db.query("apirequests", cols, "", args, null, null, null, null);
    if (cur == null)
        return list;

    do {
        APIRequest req = new APIRequest(cur);
        list.add(req);
        Log.d(TAG, "Queueing request: " + req.query);
    } while (cur.moveToNext() != false);
    cur.close();

    return list;
}

From source file:com.android.exchange.EasOutboxService.java

@Override
public void run() {
    setupService();/*  ww w .  j ava2 s  .  c om*/
    File cacheDir = mContext.getCacheDir();
    try {
        mDeviceId = SyncManager.getDeviceId();
        Cursor c = mContext.getContentResolver().query(Message.CONTENT_URI, Message.ID_COLUMN_PROJECTION,
                MAILBOX_KEY_AND_NOT_SEND_FAILED, new String[] { Long.toString(mMailbox.mId) }, null);
        try {
            while (c.moveToNext()) {
                long msgId = c.getLong(0);
                if (msgId != 0) {
                    int result = sendMessage(cacheDir, msgId);
                    // If there's an error, it should stop the service; we will distinguish
                    // at least between login failures and everything else
                    if (result == EmailServiceStatus.LOGIN_FAILED) {
                        mExitStatus = EXIT_LOGIN_FAILURE;
                        return;
                    } else if (result == EmailServiceStatus.REMOTE_EXCEPTION) {
                        mExitStatus = EXIT_EXCEPTION;
                        return;
                    }
                }
            }
        } finally {
            c.close();
        }
        mExitStatus = EXIT_DONE;
    } catch (IOException e) {
        mExitStatus = EXIT_IO_ERROR;
    } catch (Exception e) {
        userLog("Exception caught in EasOutboxService", e);
        mExitStatus = EXIT_EXCEPTION;
    } finally {
        userLog(mMailbox.mDisplayName, ": sync finished");
        userLog("Outbox exited with status ", mExitStatus);
        SyncManager.done(this);
    }
}