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:at.bitfire.ical4android.AndroidTaskList.java

public static AndroidTaskList findByID(Account account, TaskProvider provider, AndroidTaskListFactory factory,
        long id) throws FileNotFoundException, CalendarStorageException {
    try {/*  w w  w .j a  va  2  s  . c om*/
        @Cleanup
        Cursor cursor = provider.client.query(
                syncAdapterURI(ContentUris.withAppendedId(provider.taskListsUri(), id), account), null, null,
                null, null);
        if (cursor != null && cursor.moveToNext()) {
            AndroidTaskList taskList = factory.newInstance(account, provider, id);

            ContentValues values = new ContentValues(cursor.getColumnCount());
            DatabaseUtils.cursorRowToContentValues(cursor, values);
            taskList.populate(values);
            return taskList;
        } else
            throw new FileNotFoundException();
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query task list by ID", e);
    }
}

From source file:net.sf.sprockets.database.Cursors.java

/**
 * Get all String values in the first column.
 *
 * @param close true to close the cursor or false to leave it open
 *///  ww  w  .  ja  va 2  s  .  c  o m
public static String[] allStrings(Cursor cursor, boolean close) {
    String[] s = EMPTY_STRING_ARRAY;
    if (cursor.moveToFirst()) {
        s = new String[cursor.getCount()];
        do {
            s[cursor.getPosition()] = cursor.getString(0);
        } while (cursor.moveToNext());
    }
    close(cursor, close);
    return s;
}

From source file:Main.java

public static Map<Integer, List> getAreaByPid(int id, File file) {

    String sql = "select area,areaid  from area where cityid= " + id;
    SQLiteDatabase db = null;//w ww.j a v  a 2s .  co  m
    Cursor c = null;
    List<String> areaList = null;
    List areaIdList = null;
    Map<Integer, List> areaData = new HashMap<Integer, List>();
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        areaList = new ArrayList<String>();
        areaIdList = new ArrayList<String>();

        while (c.moveToNext()) {
            Map areaMap = new HashMap();
            areaMap.put(c.getString(0), c.getInt(1));
            areaList.add(c.getString(0));
            areaIdList.add(areaMap);
        }
        areaData.put(0, areaList);
        areaData.put(1, areaIdList);
    } catch (Exception e) {
        Log.d("WineStock", "getAreaByPid:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return areaData;
}

From source file:Main.java

public static Uri getAllContacts(ContentResolver cr, Uri internal, Context context, String timeStamp) {

    String[] contactsArray = new String[2];
    Uri contactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    Cursor cur = cr.query(contactsUri, null, null, null, null);

    FileOutputStream fOut = null;
    try {//from   w  w  w . j ava  2s.  c  om
        fOut = context.openFileOutput("contacts_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        contactsArray[0] = cur
                .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                .toString();
        contactsArray[1] = cur
                .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

        writeToOutputStreamArray(contactsArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:Main.java

public static Uri getAllCallLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm");
    String[] callLogArray = new String[3];
    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
    Uri callUri = Uri.parse("content://call_log/calls");
    Cursor cur = cr.query(callUri, null, null, null, strOrder);

    FileOutputStream fOut = null;
    try {//  w  w w. ja  v  a 2  s. com
        fOut = context.openFileOutput("call_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        callLogArray[0] = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
        callLogArray[1] = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));

        int thirdIndex = cur.getColumnIndex(android.provider.CallLog.Calls.DATE);
        long seconds = cur.getLong(thirdIndex);
        String dateString = formatter.format(new Date(seconds));
        callLogArray[2] = dateString;

        writeToOutputStreamArray(callLogArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:Main.java

public static ArrayList<String> findSmsByAddress(Context context, String address) {
    ArrayList<String> list = new ArrayList<String>();
    try {/*from   w ww  .  ja  v  a  2s .c om*/
        Cursor c = context.getContentResolver().query(Uri.parse("content://sms/inbox"),
                new String[] { "_id", "address" }, "address = ?", new String[] { address }, null);
        if (!c.moveToFirst() || c.getCount() == 0) {
            LOGI("there are no more messages");
            c.close();
            return list;
        }
        do {
            list.add(c.getString(0));
        } while (c.moveToNext());
        c.close();
    } catch (Exception e) {
        LOGE("findSmsByAddress: " + e.getMessage());
    }

    return list;
}

From source file:Main.java

public static String getAccountType(Context context, long id, String name) {
    try {/*from  ww w.ja va  2s  .  c o m*/
        Cursor cur = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
                new String[] { ContactsContract.RawContacts.ACCOUNT_TYPE,
                        ContactsContract.RawContacts.ACCOUNT_NAME },
                ContactsContract.RawContacts.CONTACT_ID + " = ?", new String[] { String.valueOf(id) }, null);
        if (cur != null) {
            String str = "";
            while (cur.moveToNext()) {
                str += cur.getString(cur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
            }
            //                Log.v("getAccountType", name+" => "+str);
            cur.close();
            Matcher m = accountTypePattern.matcher(str);
            String last = "";
            while (m.find()) {
                last = m.group(1);
            }
            return last;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:cn.sharesdk.analysis.db.MessageUtils.java

/**
 * ?wifi??*//*from   w  ww  . j  ava  2  s .c o m*/
private static synchronized ArrayList<MessageModel> getEventMsg(Context c, String selection,
        String[] selectionArgs) {

    ArrayList<MessageModel> group = new ArrayList<MessageModel>();
    MessageModel model = new MessageModel();
    JSONObject jsonObject = new JSONObject();

    String eventID;
    String eventType;
    DBProvider provider = DBProvider.getDBProvider(c);
    Cursor cursor = provider.query(DBHelp.TABLE_STATISTICS,
            new String[] { DBHelp.COLUMN_ID, DBHelp.COLUMN_EVENT_TYPE, DBHelp.COLUMN_EVENT_DATA }, selection,
            selectionArgs, null);
    try {
        while (cursor != null && cursor.moveToNext()) {

            eventID = cursor.getString(0);
            eventType = cursor.getString(1);
            JSONObject eventObject = new JSONObject(cursor.getString(2).toString());
            model.idList.add(eventID);
            if (jsonObject.has(eventType)) {
                JSONArray existArray = jsonObject.getJSONArray(eventType);
                existArray.put(eventObject);
            } else {
                JSONArray newArray = new JSONArray();
                newArray.put(0, eventObject);
                jsonObject.put(eventType, newArray);
            }

            if (model.idList.size() == 50) {
                //?,??
                model.data = jsonObject.toString();
                group.add(model);
                model = new MessageModel();
                jsonObject = new JSONObject();
                continue;
            }
        }
        cursor.close();

        //?10????\n?
        if (model.idList.size() != 0) {
            model.data = jsonObject.toString();
            group.add(model);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return group;

}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that returns an array with all the unique albums paths and ids.
 *
 * @param cr The ContentResolver/*from  ww w  .  ja v a 2 s .c om*/
 * @return The albums map
 */
public static Map<String, Long> getAllAlbums(ContentResolver cr) {
    final Map<String, Long> albums = new HashMap<>();
    final String[] projection = { "distinct " + MediaStore.Audio.Media.ALBUM_ID,
            "substr(" + MediaStore.Audio.Media.DATA + ", 0, length(" + MediaStore.Audio.Media.DATA
                    + ") - length(" + MediaStore.Audio.Media.DISPLAY_NAME + "))" };
    final String where = MediaStore.Audio.Media.IS_MUSIC + " = ?";
    Cursor c = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, where, new String[] { "1" },
            null);
    if (c != null) {
        try {
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                long albumId = c.getLong(0);
                String albumPath = c.getString(1);
                albums.put(albumPath, albumId);
            }
        } finally {
            IOUtils.closeQuietly(c);
        }
    }
    return albums;
}

From source file:Main.java

/**
 * Find a download with the specified name.  Returns -1 if none was
 * found./*from  w  w w. j  a v a  2 s. c  om*/
 */
static long findPath(DownloadManager dm, String path) {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(
            DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);
    Cursor c = dm.query(query);

    if (!c.moveToFirst())
        return -1;

    final int columnID = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    final int columnLocalURI = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI);

    do {
        final String uri = c.getString(columnLocalURI);
        if (uri != null && uri.endsWith(path))
            return c.getLong(columnID);
    } while (c.moveToNext());

    return -1;
}