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:Main.java

/**
 * Return the filename from a uri./*w  w w.ja  v a 2s .  c o m*/
 */
public static String getFilename(Context c, Uri uri) {
    try {
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            return uri.getLastPathSegment();
        } else if (scheme.equals("content")) {
            String[] proj = { MediaStore.Files.FileColumns.DISPLAY_NAME };
            Cursor cursor = c.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME);
                cursor.moveToFirst();
                return cursor.getString(columnIndex);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:fr.mixit.android.io.RemoteSessionsHandler.java

private static boolean isSessionTagsUpdated(Uri uri, JSONArray tags, ContentResolver resolver)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, TagsQuery.PROJECTION, null, null, null);
    try {//  ww w  . j  a  v a 2s  . c  o  m
        if (!cursor.moveToFirst())
            return false;
        return cursor.getCount() != tags.length();
    } finally {
        cursor.close();
    }
}

From source file:Main.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {//from w  w w  .  j av  a2  s.co m
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:nz.co.wholemeal.christchurchmetro.Stop.java

private static ArrayList<Stop> doArrayListQuery(Context context, String query) {
    ArrayList<Stop> stops = new ArrayList<Stop>();

    Log.d(TAG, "query: " + query);

    DatabaseHelper databaseHelper = new DatabaseHelper(context);
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    Cursor cursor = database.rawQuery(query, null);

    try {//from  ww  w .ja va  2  s  .  co  m
        if (cursor.moveToFirst()) {
            do {
                Stop stop = new Stop();
                stop.platformTag = cursor.getString(0);
                stop.platformNumber = cursor.getString(1);
                stop.name = cursor.getString(2);
                stop.roadName = cursor.getString(3);
                stop.latitude = cursor.getDouble(4);
                stop.longitude = cursor.getDouble(5);
                stops.add(stop);
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    Log.d(TAG, "stops.size() = " + stops.size());
    database.close();
    return stops;
}

From source file:fr.mixit.android.io.RemoteSessionsHandler.java

private static boolean isSessionSpeakersUpdated(Uri uri, JSONArray speakers, ContentResolver resolver)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, SpeakersQuery.PROJECTION, null, null, null);
    try {//from w w  w  .  j  a va2s .c om
        if (!cursor.moveToFirst())
            return false;
        return cursor.getCount() != speakers.length();
    } finally {
        cursor.close();
    }
}

From source file:com.csipsimple.utils.SipProfileJson.java

public static JSONObject serializeSipProfile(SipProfile profile, DBAdapter db) {
    JSONObject jsonProfile = serializeBaseSipProfile(profile);
    JSONArray jsonFilters = new JSONArray();

    Cursor c = db.getFiltersForAccount(profile.id);
    int numRows = c.getCount();
    c.moveToFirst();
    for (int i = 0; i < numRows; ++i) {
        Filter f = new Filter();
        f.createFromDb(c);/*from  ww  w.  jav  a 2s . c o m*/

        try {
            jsonFilters.put(i, serializeBaseFilter(f));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add fitler", e);
            e.printStackTrace();
        }
        c.moveToNext();
    }
    c.close();

    try {
        jsonProfile.put(FILTER_KEY, jsonFilters);
    } catch (JSONException e) {
        Log.e(THIS_FILE, "Impossible to add fitlers", e);
    }

    return jsonProfile;
}

From source file:Main.java

/**
 * Get {@link File} object for the given Android URI.<br>
 * Use content resolver to get real path if direct path doesn't return valid file.
 *//*from   w w w .  ja v  a2 s .c o m*/
private static File getFileFromUri(Context context, Uri uri) {

    // first try by direct path
    File file = new File(uri.getPath());
    if (file.exists()) {
        return file;
    }

    // try reading real path from content resolver (gallery images)
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String realPath = cursor.getString(column_index);
            file = new File(realPath);
        }
    } catch (Exception ignored) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return file;
}

From source file:com.xfinity.ceylon_steel.controller.CustomerController.java

public static ArrayList<Customer> getCustomers(Context context) {
    ArrayList<Customer> customers = new ArrayList<Customer>();
    SQLiteDatabaseHelper databaseInstance = SQLiteDatabaseHelper.getDatabaseInstance(context);
    SQLiteDatabase database = databaseInstance.getWritableDatabase();
    Cursor customerCursor = database.rawQuery("select customerId, customerName from tbl_customer", null);
    int customerIdIndex = customerCursor.getColumnIndex("customerId");
    int customerNameIndex = customerCursor.getColumnIndex("customerName");
    for (customerCursor.moveToFirst(); !customerCursor.isAfterLast(); customerCursor.moveToNext()) {
        Customer customer = new Customer(customerCursor.getInt(customerIdIndex),
                customerCursor.getString(customerNameIndex));
        customers.add(customer);/*  www  . ja v  a 2 s  .  c o m*/
    }
    customerCursor.close();
    databaseInstance.close();
    return customers;
}

From source file:com.contentful.vault.SqliteHelper.java

static void deleteTables(SQLiteDatabase db) {
    String[] columns = new String[] { "name" };
    String selection = "type = ? AND name != ?";
    String[] args = new String[] { "table", "android_metadata" };
    Cursor cursor = db.query("sqlite_master", columns, selection, args, null, null, null);
    List<String> tables = null;
    try {//from w  w w  .  jav a  2  s  .  c  om
        if (cursor.moveToFirst()) {
            tables = new ArrayList<>();
            do {
                tables.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    if (tables != null) {
        db.beginTransaction();
        try {
            for (String table : tables) {
                db.execSQL("DROP TABLE " + escape(table));
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}

From source file:Main.java

/**
 * Returns the ID for an album.// w  ww . j av  a2 s.co m
 *
 * @param context The {@link Context} to use.
 * @param albumName The name of the album.
 * @param artistName The name of the artist
 * @return The ID for an album.
 */
public static final long getIdForAlbum(final Context context, final String albumName, final String artistName) {
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[] { BaseColumns._ID }, AlbumColumns.ALBUM + "=? AND " + AlbumColumns.ARTIST + "=?",
            new String[] { albumName, artistName }, AlbumColumns.ALBUM);
    int id = -1;
    if (cursor != null) {
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            id = cursor.getInt(0);
        }
        cursor.close();
        cursor = null;
    }
    return id;
}