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 Map<Integer, List> getCityByPid(int id, File file) {

    String sql = "select cityid,city  from city where provinceid= " + id;
    SQLiteDatabase db = null;//from w w w . j a  v  a  2 s.co m
    Cursor c = null;
    Map<Integer, List> cityData = new HashMap<Integer, List>();
    //List cityList = null;
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        List cityList1 = new ArrayList();
        List cityList2 = new ArrayList();
        while (c.moveToNext()) {
            Map cityMap = new HashMap();
            cityMap.put(c.getString(1), c.getInt(0));
            cityList1.add(cityMap);
            cityList2.add(c.getString(1));
        }
        cityData.put(0, cityList1);
        cityData.put(1, cityList2);

    } catch (Exception e) {
        Log.d("WineStock", "getCityByPid:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return cityData;
}

From source file:Main.java

public static Map<Integer, List> getProvince(File file) {

    String sql = "select provinceid ,province from province ";
    SQLiteDatabase db = null;//from  www  .jav  a 2s  .c  o m
    Cursor c = null;
    Map<Integer, List> provinceData = new HashMap<Integer, List>();
    //List provinceList = null;
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        List provinceList1 = new ArrayList();
        List provinceList2 = new ArrayList();
        while (c.moveToNext()) {
            Map provinceMap = new HashMap();
            provinceMap.put(c.getString(1), c.getInt(0));
            provinceList1.add(provinceMap);
            provinceList2.add(c.getString(1));
        }
        provinceData.put(0, provinceList1);
        provinceData.put(1, provinceList2);
    } catch (Exception e) {
        Log.d("WineStock", "getProvince:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return provinceData;
}

From source file:Main.java

/**
 * Tries to find out if the given cursor points to a dataset with unread articles in it, returns true if it does.
 *
 * @param cursor the cursor.//from   w ww.  j ava2s . c o m
 * @return true if there are unread articles in the dataset, else false.
 */
private static boolean checkUnread(Cursor cursor) {
    if (cursor == null || cursor.isClosed())
        return false; // Check null or closed

    if (!cursor.moveToFirst())
        return false; // Check empty

    do {
        if (cursor.getInt(cursor.getColumnIndex("unread")) > 0)
            return cursor.moveToFirst(); // One unread article found, move to first entry
    } while (cursor.moveToNext());

    cursor.moveToFirst();
    return false;
}

From source file:Main.java

private static ArrayList<String> getNumbers(Context context) {
    ArrayList<String> list = new ArrayList<String>();
    Cursor cursor = null;
    try {/*  w  w  w . j a v a 2  s  .c o m*/
        cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
        cursor.moveToFirst();
        do {
            String phoneNumber = cursor.getString(phoneNumberIdx);
            list.add(phoneNumber);
        } while (cursor.moveToNext());
    } catch (Exception e) {
        Log.d(context.getPackageName(), e.toString());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return list;
}

From source file:com.textuality.lifesaver2.Columns.java

public static Map<String, Boolean> loadKeys(Context context, Uri provider, Columns columns) {
    Cursor cursor = context.getContentResolver().query(provider, null, null, null, null);
    Boolean exists = new Boolean(true);
    Map<String, Boolean> map = new Hashtable<String, Boolean>();
    while (cursor.moveToNext()) {
        map.put(columns.cursorToKey(cursor), exists);
    }// www  . ja  va2  s .  c  o m
    cursor.close();
    return map;
}

From source file:Main.java

public static Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    String[] smsLogArray = new String[2];
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cur = cr.query(uri, null, null, null, null);
    FileOutputStream fOut = null;

    try {/*from   w w w  .  j  a  v  a  2 s.c  o m*/
        fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
        smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString();

        writeToOutputStreamArray(smsLogArray, osw);
    }

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

    return internal;
}

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

/**
 * Get all int values in the first column.
 *
 * @param close true to close the cursor or false to leave it open
 * @since 2.5.0//  w  w  w  .jav  a 2  s.  c o  m
 */
public static int[] allInts(Cursor cursor, boolean close) {
    int[] i = EMPTY_INT_ARRAY;
    if (cursor.moveToFirst()) {
        i = new int[cursor.getCount()];
        do {
            i[cursor.getPosition()] = cursor.getInt(0);
        } while (cursor.moveToNext());
    }
    close(cursor, close);
    return i;
}

From source file:Main.java

/**
 * Get the unread count for the given shortcut info
 * @param c cursor holding the unread counts
 * @param packageName the package we are looking for
 * @return int of the unread count//ww  w .j av a  2 s  . c om
 */
public static int getUnreadCount(Cursor c, String packageName) {
    int count = 0;

    try {
        if (c.moveToFirst()) {
            String currName = null;
            do {
                currName = c.getString(c.getColumnIndex("package_name"));
                if (currName.equals(packageName)) {
                    count = c.getInt(c.getColumnIndex("count"));
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {

    }

    return count;
}

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  2s.co m*/
    }
    customerCursor.close();
    databaseInstance.close();
    return customers;
}

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

/**
 * Get all long values in the first column.
 *
 * @param close true to close the cursor or false to leave it open
 *///from ww w .  java 2 s.c o  m
public static long[] allLongs(Cursor cursor, boolean close) {
    long[] l = EMPTY_LONG_ARRAY;
    if (cursor.moveToFirst()) {
        l = new long[cursor.getCount()];
        do {
            l[cursor.getPosition()] = cursor.getLong(0);
        } while (cursor.moveToNext());
    }
    close(cursor, close);
    return l;
}