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 String uriToString(Context context, Uri uri) {
    String scheme = uri.getScheme();
    if (scheme != null) {
        if (scheme.equals("http") || scheme.equals("https")) {
            return uri.toString();
        } else if (scheme.equals("content") || scheme.equals("file")) {
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[] { OpenableColumns.DISPLAY_NAME }, null, null, null);
            if (cursor.moveToNext()) {
                String name = cursor.getString(0);
                cursor.close();//w w  w. j  a  va 2  s .c om
                return name;
            }
            cursor.close();
            return uri.getPath();
        }
    }
    return uri.toString();
}

From source file:Main.java

public static HashSet<String> getUserContacts(ContentResolver cr) {
    HashSet<String> contactsList = new HashSet<String>();
    //ContentResolver cr = 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));
            // We could store names and numbers in a dictionary if we ever needed that
            // 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 the number stored isn't long enough to be in our database then don't bother.
                    if (phoneNo.length() >= 10) {
                        // Use regex to remove '+', spaces, '(', and ')' and '-' form user contacts to match database format
                        String formattedPhoneNo = phoneNo.replaceAll(DELIMITERS, "");
                        // Remove the '1' in front of the number
                        if (formattedPhoneNo.charAt(0) == '1') {
                            formattedPhoneNo = formattedPhoneNo.substring(1);
                        }//w  w  w .j av  a  2  s . c om
                        contactsList.add(formattedPhoneNo);
                    }
                }
                pCur.close();
            }
        }
    }
    return contactsList;
}

From source file:Main.java

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

From source file:Main.java

public static java.util.List<String> getLabelsFromInt(Cursor c) {
    c.moveToFirst();/*from  w w  w.  jav a2 s .  com*/
    ArrayList<String> res = new ArrayList<>();
    for (int i = 0; i < c.getCount(); i++) {
        res.add(i, String.valueOf(c.getInt(0)));
        c.moveToNext();
    }
    return res;
}

From source file:Main.java

public static void addToCalendar(Activity context, Long beginTime, String title) {
    ContentResolver cr = context.getContentResolver();

    Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
    Long time = new Date(beginTime).getTime();
    ContentUris.appendId(builder, time - 10 * 60 * 1000);
    ContentUris.appendId(builder, time + 10 * 60 * 1000);

    String[] projection = new String[] { "title", "begin" };
    Cursor cursor = cr.query(builder.build(), projection, null, null, null);

    boolean exists = false;
    if (cursor != null) {
        while (cursor.moveToNext()) {
            if ((time == cursor.getLong(1)) && title.equals(cursor.getString(0))) {
                exists = true;/*w  w w .j  av a 2  s .  c o m*/
            }
        }
    }

    if (!exists) {
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", time);
        intent.putExtra("allDay", false);
        intent.putExtra("endTime", time + 60 * 60 * 1000);
        intent.putExtra("title", title);
        context.startActivity(intent);
    } else {
        Toast.makeText(context, "Event already exist!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

public static String getMessageCount(Context context, String id) {
    String res = null;//from  ww w.  j a  v  a2s .co m
    try {
        final String[] projection = new String[] { "_id", "message_count" };
        Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
        Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
        if (query != null) {
            boolean find = false;
            while (query.moveToNext() && !find) {
                if (query.getString(query.getColumnIndex("_id")).equals(id)) {
                    res = query.getString(query.getColumnIndex("message_count"));
                    //                        Log.v("getMessageCount", "find, nb_sms = "+res);
                    find = true;
                }
            }
            query.close();
        }
    } catch (Exception e) {
        //            Log.v("getMessageCount", "Erreur");
        e.printStackTrace();
    }
    return res;
}

From source file:Main.java

public static Map<String, Integer> getProvince(SQLiteDatabase db, String tableName) {
    Map<String, Integer> provinceMap = new LinkedHashMap<String, Integer>();
    Cursor cursor = db.query(tableName, new String[] { "DQX_DQXX01", "DQXX02" }, "DQXX03=?",
            new String[] { "1" }, null, null, "DQX_DQXX01 ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {
            provinceMap.put(cursor.getString(1), cursor.getInt(0));
        }//from  w w w . ja  v a 2s .c  o  m
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return provinceMap;
}

From source file:Main.java

public static ArrayList<ContentValues> getProvince(SQLiteDatabase db) {
    ArrayList<ContentValues> list = new ArrayList<ContentValues>();
    ContentValues values = null;/*  w  w  w  . j  a va  2s  . c  om*/
    Cursor cursor = db.query(TABLE_NAME, new String[] { "DQXX01", "DQXX02" }, "DQXX03=?", new String[] { "1" },
            null, null, "DQX_DQXX01 ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {
            values = new ContentValues();
            values.put("province_id", cursor.getInt(0));
            values.put("province", cursor.getString(1));
            list.add(values);
        }
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list;
}

From source file:Main.java

public static Map<String, Integer> getArea(SQLiteDatabase db, String tableName, int dqx_dqxx01) {
    Map<String, Integer> areaMap = new LinkedHashMap<String, Integer>();
    Cursor cursor = db.query(tableName, new String[] { "DQXX02", "DQXX01" }, "DQX_DQXX01=?",
            new String[] { "" + dqx_dqxx01 }, null, null, "DQXX01 ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {
            areaMap.put(cursor.getString(0), cursor.getInt(1));
        }/*from   www. j a  v  a  2 s  .  c  o m*/
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return areaMap;
}

From source file:Main.java

public static synchronized void deleteCalllogByPhone(Context context, String number) {
    if (TextUtils.isEmpty(number))
        return;/*w w w  . j  a va 2 s  .  c  o m*/
    try {
        Cursor cursor = context.getContentResolver().query(Calls.CONTENT_URI, null, Calls.NUMBER + "=?",
                new String[] { number }, Calls.DATE + " desc");
        if (cursor != null) {
            if (cursor.moveToNext()) {
                long _id = cursor.getLong(cursor.getColumnIndex(Calls._ID));
                context.getContentResolver().delete(Calls.CONTENT_URI, Calls._ID + "=?",
                        new String[] { String.valueOf(_id) });
            }
            cursor.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}