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

public static <T> ArrayList<T> cursorToList(Cursor cursor, Class<T> modelClass) {
    ArrayList<T> items = new ArrayList<T>();
    if (!isCursorEmpty(cursor)) {
        while (cursor.moveToNext()) {
            final T model = buildModel(modelClass, cursor);
            items.add(model);//from  www .j  ava 2  s  . co m
        }
    }
    return items;
}

From source file:Main.java

public static String query(Context context, Uri uri) {
    if (context == null || uri == null)
        return "";
    Cursor cursor = context.getContentResolver().query(uri,
            new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
    cursor.moveToNext();
    return cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
}

From source file:Main.java

public static String getNewThreadID(Context context) {
    int new_id = 0;
    final String[] projection = new String[] { "_id" };
    Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
    Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
    if (query != null) {
        while (query.moveToNext()) {
            int tmp = query.getInt(query.getColumnIndexOrThrow("_id")) + 1;
            if (tmp > new_id) {
                new_id = tmp;
            }//w  ww. ja  v a 2s.  c om
        }
        query.close();
    }
    return "" + new_id;
}

From source file:Main.java

/**
 * get image path by uri//from  ww  w.java2 s.c  om
 *
 * @param context context
 * @param uri     uri
 * @return image path
 */
public static String query(Context context, Uri uri) {
    Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
            null);
    cursor.moveToNext();
    String path = cursor.getString(cursor.getColumnIndex(ImageColumns.DATA));
    cursor.close();
    return path;
}

From source file:Main.java

public static String getImagePath(Context context, String id) {
    String path = null;//  ww w  .ja va 2 s.  c om
    Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, Media._ID + "=?",
            new String[] { id }, null);
    while (cursor.moveToNext()) {
        path = cursor.getString(cursor.getColumnIndex(Media.DATA));
    }
    cursor.close();
    return path;
}

From source file:Main.java

public static List<String> displayContacts(ContentResolver contentResolver) {

    List<String> contacts = new ArrayList<String>();
    ContentResolver cr = contentResolver;
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    contacts.add(name + " : " + phoneNo);
                }//from ww w  .  j  ava2s .  c  om
                pCur.close();
            }
        }
    }
    return contacts;
}

From source file:Main.java

public static boolean hasContact(Context mContext, String contact) {
    ContentResolver cr = mContext.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    if (phoneNo.contains(contact) || contact.contains(phoneNo)) {
                        return true;
                    }//from   w  w w .  j  av  a  2 s . c o  m
                }
                pCur.close();
            }
        }
    }
    return false;
}

From source file:Main.java

public static String getSdCardPath(ContentResolver cr, Uri uri, String defaultPath) {
    ArrayList files = new ArrayList();
    if (uri != null) {
        Cursor mCursor = cr.query(uri, null, null, null, null);
        if (mCursor != null) {
            while (mCursor.moveToNext()) {
                String path = mCursor.getString(mCursor.getColumnIndexOrThrow("_data"));
                if (!path.contains(defaultPath)) {
                    String parentFolder = (new File(path)).getParent();
                    if (!files.contains(parentFolder)) {
                        files.add(parentFolder);
                    }/*from  ww  w  . j ava2  s  . c o m*/
                }
            }
        }
    }

    return getCommonSubStringFromList(files);
}

From source file:Main.java

public static Uri getUriFromPath(Context context, String path) {

    String fileName = "file:///sdcard/DCIM/Camera/2013_07_07_12345.jpg";
    Uri fileUri = Uri.parse(fileName);/*from   w w w.ja  v  a 2s .  co  m*/
    String filePath = fileUri.getPath();
    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
            "_data = '" + filePath + "'", null, null);
    cursor.moveToNext();
    int id = cursor.getInt(cursor.getColumnIndex("_id"));
    Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);

    return uri;
}

From source file:Main.java

public static int findPrimaryKey(SQLiteDatabase db, String tableName, String address) {
    int key = -1;
    Cursor cursor = db.query(tableName, new String[] { "DQXX01" }, "DQXX05=?", new String[] { address }, null,
            null, null);//from  w  ww .  j av a  2s  .c  om
    if (cursor != null) {
        if (cursor.moveToNext()) {
            key = cursor.getInt(0);
        }
    }
    return key;
}