Example usage for android.database Cursor moveToFirst

List of usage examples for android.database Cursor moveToFirst

Introduction

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

Prototype

boolean moveToFirst();

Source Link

Document

Move the cursor to the first row.

Usage

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static HashSet<String> getBannedTrolls(Context context) {

    HashSet<String> bannedTrolls = null;

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

    String q = "SELECT name FROM banned_users WHERE bandisabled=0";

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

    int count = c.getCount();
    if (count > 0) {

        bannedTrolls = new HashSet<String>(c.getColumnCount());
        c.moveToFirst();

        for (int i = 0; i < count; i++) {
            bannedTrolls.add(c.getString(0));
            c.moveToNext();/*from w w  w . j av  a  2  s  .  com*/
        }
    }

    c.close();
    dbwrite.close();
    db.close();

    if (bannedTrolls == null)
        bannedTrolls = new HashSet<String>(0);
    return bannedTrolls;
}

From source file:com.digitalarx.android.syncadapter.ContactSyncAdapter.java

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {//from   w  ww . j a va  2 s .co m
    setAccount(account);
    setContentProviderClient(provider);
    Cursor c = getLocalContacts(false);
    if (c.moveToFirst()) {
        do {
            String lookup = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            String a = getAddressBookUri();
            String uri = a + lookup + ".vcf";
            FileInputStream f;
            try {
                f = getContactVcard(lookup);
                HttpPut query = new HttpPut(uri);
                byte[] b = new byte[f.available()];
                f.read(b);
                query.setEntity(new ByteArrayEntity(b));
                fireRawRequest(query);
            } catch (IOException e) {
                e.printStackTrace();
                return;
            } catch (OperationCanceledException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (AuthenticatorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } while (c.moveToNext());
        // } while (c.moveToNext());
    }

}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static HashSet<String> getStarredSubjectsSet(Context context) {
    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbread = db.getReadableDatabase();
    Cursor c;

    String query = "SELECT clean_subject FROM starred_threads";
    c = dbread.rawQuery(query, null);//w  w w  . j a  v  a  2s .c  o m
    HashSet<String> set = new HashSet<String>(c.getCount());

    c.moveToFirst();
    int count = c.getCount();
    for (int i = 0; i < count; i++) {
        set.add(c.getString(0));
        c.moveToNext();
    }

    c.close();
    dbread.close();
    db.close();
    return set;
}

From source file:com.android.emailcommon.provider.Account.java

public static Account restoreAccountWithAddress(Context context, String emailAddress,
        ContentObserver observer) {//w w w.j  ava  2  s. c o  m
    final Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { AccountColumns._ID },
            AccountColumns.EMAIL_ADDRESS + "=?", new String[] { emailAddress }, null);
    try {
        if (c == null || !c.moveToFirst()) {
            return null;
        }
        final long id = c.getLong(c.getColumnIndex(AccountColumns._ID));
        return restoreAccountWithId(context, id, observer);
    } finally {
        if (c != null) {
            c.close();
        }
    }
}

From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java

/**
 * Queries the media store to find out what the file path is for the Uri we supply
 *
 * @param contentUri the Uri of the audio/image/video
 * @param  cordova) the current applicaiton context
 * @return the full path to the file//www .j  a  v  a 2  s  .com
 */
@SuppressWarnings("deprecation")
protected static String getRealPathFromURI(Uri contentUri, CordovaInterface cordova) {
    String[] proj = { _DATA };
    Cursor cursor = cordova.getActivity().managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(_DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

From source file:com.codebutler.farebot.transit.SuicaTransitData.java

/**
 * ?????????/*w w w .  j ava2 s .c o  m*/
 * <pre>http://sourceforge.jp/projects/felicalib/wiki/suica???????</pre>
 *
 * @param regionCode 
 * @param lineCode 
 * @param stationCode 
 * @return ????????0????1????2??????
 */
private static Station getRailStation(int regionCode, int lineCode, int stationCode) {
    int areaCode = (regionCode >> 6);

    try {
        SQLiteDatabase db = FareBotApplication.getInstance().getSuicaDBUtil().openDatabase();
        Cursor cursor = db.query(TABLE_STATIONCODE, COLUMNS_STATIONCODE,
                String.format("%s = ? AND %s = ? AND %s = ?", COLUMN_AREACODE, COLUMN_LINECODE,
                        COLUMN_STATIONCODE),
                new String[] { String.valueOf(areaCode & 0xFF), String.valueOf(lineCode & 0xFF),
                        String.valueOf(stationCode & 0xFF) },
                null, null, COLUMN_ID);

        if (!cursor.moveToFirst()) {
            Log.w("SuicaTransitData",
                    String.format("FAILED get rail company: r: 0x%s a: 0x%s l: 0x%s s: 0x%s",
                            Integer.toHexString(regionCode), Integer.toHexString(areaCode),
                            Integer.toHexString(lineCode), Integer.toHexString(stationCode)));

            return null;
        }

        // FIXME: Figure out a better way to deal with i18n.
        boolean isJa = Locale.getDefault().getLanguage().equals("ja");
        String companyName = cursor
                .getString(cursor.getColumnIndex(isJa ? COLUMN_COMPANYNAME : COLUMN_COMPANYNAME_EN));
        String lineName = cursor.getString(cursor.getColumnIndex(isJa ? COLUMN_LINENAME : COLUMN_LINENAME_EN));
        String stationName = cursor
                .getString(cursor.getColumnIndex(isJa ? COLUMN_STATIONNAME : COLUMN_STATIONNAME_EN));
        String latitude = cursor.getString(cursor.getColumnIndex(COLUMN_LATITUDE));
        String longitude = cursor.getString(cursor.getColumnIndex(COLUMN_LONGITUDE));
        return new Station(companyName, lineName, stationName, null, latitude, longitude);

    } catch (Exception e) {
        Log.e("SuicaStationProvider", "Error in getRailStation", e);
        return null;
    }
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static String[] getSubscribedGroups(Context context) {
    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbread = db.getReadableDatabase();

    Cursor cur = dbread.rawQuery("SELECT name FROM subscribed_groups", null);
    int c = cur.getCount();
    String[] subscribed = null;// w w  w. jav a  2 s. co m
    if (c > 0) {
        subscribed = new String[c];

        cur.moveToFirst();
        for (int i = 0; i < c; i++) {
            subscribed[i] = cur.getString(0);
            cur.moveToNext();
        }
    }

    cur.close();
    dbread.close();
    db.close();
    return subscribed;
}

From source file:com.lee.sdk.utils.Utils.java

/**
 * ?????????//from   w  ww  . jav a 2  s. c o m
 * 
 * @param context {@link Context}
 * @param shortcutName ??
 * @param packageName ??
 * @param uri {@link Uri}
 * 
 * @return ???DB?true
 */
private static boolean hasShortcut(Context context, String shortcutName, String packageName, Uri uri) {
    if (context == null || TextUtils.isEmpty(shortcutName) || TextUtils.isEmpty(packageName) || uri == null) {
        return true;
    }

    boolean res = false;

    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(uri, new String[] { "title", "intent" }, "title=?",
                new String[] { shortcutName }, null);

        if (cursor != null && cursor.moveToFirst()) {
            int index = cursor.getColumnIndex("intent");
            if (index >= 0 && index < cursor.getColumnCount()) {
                do {
                    String intentString = cursor.getString(index);
                    if (intentString != null && intentString.contains(packageName)) {
                        res = true;
                        break;
                    }
                } while (cursor.moveToNext());
            }
        }

    } catch (Exception e) {
        res = true;
    } finally {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }

    return res;
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void banThread(String group, String clean_subject, Context context) {
    int groupid = getGroupIdFromName(group, context);

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

    // First, check if it already is on the banned_threads table (it could be with unbanned=1)
    Cursor c = dbwrite.rawQuery("SELECT _id FROM banned_threads " + " WHERE subscribed_group_id=" + groupid
            + " AND clean_subject=" + esc(clean_subject), null);

    if (c.getCount() > 0) { // Existed
        c.moveToFirst();
        dbwrite.execSQL("UPDATE banned_threads SET bandisabled=0 WHERE _id=" + c.getInt(0));

    } else {/*from   w w  w  .j  av  a 2s  .c o m*/
        // New troll goes down to the pit
        ContentValues cv = new ContentValues();
        cv.put("subscribed_group_id", groupid);
        cv.put("bandisabled", 0);
        cv.put("clean_subject", clean_subject);
        dbwrite.insert("banned_threads", null, cv);
    }

    // Mark all the messages from the thread as read so they get cleaned later
    dbwrite.execSQL("UPDATE headers SET read=1, read_unixdate=" + System.currentTimeMillis()
            + " WHERE subscribed_group_id=" + groupid + " AND clean_subject=" + esc(clean_subject));

    c.close();
    dbwrite.close();
    db.close();
}

From source file:ro.weednet.contactssync.platform.ContactManager.java

private static long lookupContact(ContentResolver resolver, String name, String fb_id, boolean joinById,
        boolean syncAll) {
    Cursor c;

    if (joinById) {
        c = resolver.query(ContactsContract.Data.CONTENT_URI, //table
                new String[] { ContactsContract.Data.RAW_CONTACT_ID }, //select (projection)
                ContactsContract.Data.MIMETYPE + "=? AND " + CommonDataKinds.Note.NOTE + " LIKE ?", //where
                new String[] { ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE, "%" + fb_id + "%" }, //params
                null //sort
        );//  w  ww. j  ava  2 s. c  o  m
        try {
            if (c != null && c.moveToFirst()) {
                return c.getLong(0);
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    if (syncAll) {
        return -1;
    }

    c = resolver.query(Contacts.CONTENT_URI, //table
            new String[] { Contacts._ID }, //select (projection)
            Contacts.DISPLAY_NAME + "=?", //where
            new String[] { name }, //params
            null //sort
    );
    try {
        if (c != null && c.getCount() > 0) {
            return 0;
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }

    return -1;
}