Example usage for android.database Cursor getCount

List of usage examples for android.database Cursor getCount

Introduction

In this page you can find the example usage for android.database Cursor getCount.

Prototype

int getCount();

Source Link

Document

Returns the numbers of rows in the cursor.

Usage

From source file:Main.java

@SuppressWarnings("deprecation")
public static String getAbsoluteImagePath(Activity context, Uri uri) {
    String imagePath = "";
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.managedQuery(uri, proj, // Which columns to
            // return
            null, // WHERE clause; which rows to return (all rows)
            null, // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
            imagePath = cursor.getString(column_index);
        }// w  w  w.  j  a v  a2s.  c  o m
    }

    return imagePath;
}

From source file:info.guardianproject.otr.app.im.app.DatabaseUtils.java

public static boolean doesAvatarHashExist(ContentResolver resolver, Uri queryUri, String jid, String hash) {

    StringBuilder buf = new StringBuilder(Imps.Avatars.CONTACT);
    buf.append("=?");
    buf.append(" AND ");
    buf.append(Imps.Avatars.HASH);/* ww w . j a va2 s .co m*/
    buf.append("=?");

    String[] selectionArgs = new String[] { jid, hash };

    Cursor cursor = resolver.query(queryUri, null, buf.toString(), selectionArgs, null);
    if (cursor == null)
        return false;
    try {
        return cursor.getCount() > 0;
    } finally {
        cursor.close();
    }
}

From source file:im.delight.android.baselib.Social.java

/**
 * Whether the given person (represented by phone number) is known on the current device (i.e. in the address book) or not
 *
 * @param context the Context reference to get the ContentResolver from
 * @param phoneNumber the phone number to look up
 * @return whether the phone number is in the contacts list (true) or not (false)
 *///from w ww  . j a  v  a  2  s  .c  o  m
public static boolean isPersonKnown(Context context, String phoneNumber) {
    try {
        Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        Cursor phoneEntries = context.getContentResolver().query(phoneUri,
                new String[] { android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
        return phoneEntries.getCount() > 0;
    } catch (Exception e) {
        return false;
    }
}

From source file:totalcross.android.ConnectionManager4A.java

public static void setDefaultConfiguration(int type, String cfg) {
    if (cfg == null)
        cfg = "";

    switch (type) {
    case GPRS: {//from   w  w  w.  j a v  a  2s. c o  m
        int id = -1;
        ContentResolver contentResolver = Launcher4A.loader.getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, new String[] { "_id" },
                "apn = ? and user = ? and password = ?", new String[] { "tim.br", "tim", "tim" }, null);
        if (cursor == null || cursor.getCount() <= 0) {
            TelephonyManager tel = (TelephonyManager) Launcher4A.loader
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String networkOperator = tel.getNetworkOperator();

            if (networkOperator != null && networkOperator.length() > 0) {
                int mcc = Integer.parseInt(networkOperator.substring(0, 3));
                int mnc = Integer.parseInt(networkOperator.substring(3));
                ContentValues values = new ContentValues();
                values.put("apn", "tim.br");
                values.put("user", "tim");
                values.put("password", "tim");
                values.put("mcc", mcc);
                values.put("mnc", mnc);
                values.put("numeric", mcc + "" + mnc);
                contentResolver.insert(CONTENT_URI, values);
                cursor = contentResolver.query(CONTENT_URI, new String[] { "_id" },
                        "apn = ? and user = ? and password = ?", new String[] { "tim.br", "tim", "tim" }, null);
            }
        }
        if (cursor == null)
            return;
        if (cursor.moveToFirst())
            id = cursor.getInt(0);
        cursor.close();

        if (id > -1) {
            ContentValues values = new ContentValues();
            //See /etc/apns-conf.xml. The TelephonyProvider uses this file to provide
            //content://telephony/carriers/preferapn URI mapping
            values.put("apn_id", id);
            contentResolver.update(PREFERRED_APN_URI, values, null, null);
            cursor = contentResolver.query(PREFERRED_APN_URI, new String[] { "name", "apn" }, "_id=" + id, null,
                    null);
            if (cursor != null)
                cursor.close();
        }
    }
        break;
    case WIFI:
        connWIFI = connWIFIPrefix + cfg;
        break;
    //default:
    //   throw new IllegalArgumentException("Invalid value for argument 'type'");
    }
}

From source file:Main.java

/**
 * Consolidates the file path determination functionality of the various
 * media prompts. Beginning with KitKat, the responses use a different
 * mechanism and needs a lot of special handling.
 *
 * @param ctxt//from  w w w  .  j ava 2  s . c om
 * @param uri
 * @param pathKey
 * @return
 */
@SuppressLint("NewApi")
public static String getPathFromUri(Context ctxt, Uri uri, String pathKey) {

    if (Build.VERSION.SDK_INT >= 19) {
        return getPath(ctxt, uri);
    } else {
        if (uri.toString().startsWith("file")) {
            return uri.toString().substring(7);
        } else {
            String[] projection = { pathKey };
            Cursor c = null;
            try {
                c = ctxt.getContentResolver().query(uri, projection, null, null, null);
                int column_index = c.getColumnIndexOrThrow(pathKey);
                String path = null;
                if (c.getCount() > 0) {
                    c.moveToFirst();
                    path = c.getString(column_index);
                }
                return path;
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

From source file:curso.android.DAO.RespostaDAO.java

public static List<Resposta> getRespostaPergunta(Pergunta p) {
    Cursor cursor = null;
    try {/*from  ww  w.  j a  v  a2 s  .c o  m*/
        List<Resposta> questions = new ArrayList<Resposta>();

        cursor = Const.db.rawQuery("SELECT * FROM resposta WHERE ask_id = " + p.getAsk_id(), null);

        if (cursor.getCount() > 0) {
            int idIndex = cursor.getColumnIndex("asw_id");
            int askIndex = cursor.getColumnIndex("ask_id");
            int userIndex = cursor.getColumnIndex("user_id");
            int respostaIndex = cursor.getColumnIndex("asw_resposta");
            int nameIndex = cursor.getColumnIndex("user_name");
            cursor.moveToFirst();
            do {
                Long id = Long.valueOf(cursor.getInt(idIndex));
                Long ask = Long.valueOf(cursor.getString(askIndex));
                Long user = Long.valueOf(cursor.getInt(userIndex));
                String resposta = cursor.getString(respostaIndex);
                String user_name = cursor.getString(nameIndex);

                Resposta answer = new Resposta();
                answer.setAsw_id(id);
                answer.setAsk_id(ask);
                answer.setUser_id(user);
                answer.setAsw_resposta(resposta);
                answer.setUser_name(user_name);

                questions.add(answer);

                cursor.moveToNext();
            } while (!cursor.isAfterLast());
        }

        return questions;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:net.ccghe.emocha.model.DBAdapter.java

public static int pendingFileTransfersNum() {
    int result = 0;
    Cursor c;

    c = sDB.query(TABLE_DOWNLOADS, new String[] { "1" }, FILTER_DOWNLOAD, null, null, null, null);
    if (c != null) {
        result += c.getCount();
    }/*from w ww .j  a  v  a  2 s  .co m*/
    c.close();

    c = sDB.query(false, TABLE_UPLOADS, new String[] { "1" }, null, null, null, null, null, null);
    if (c != null) {
        result += c.getCount();
    }
    c.close();
    return result;
}

From source file:net.ccghe.emocha.model.DBAdapter.java

public static ArrayList<String> getFilePaths() {
    Cursor c = sDB.query(TABLE_DOWNLOADS, new String[] { "path" }, null, null, null, null, null);
    ArrayList<String> result = new ArrayList<String>();

    int numRows = c.getCount();
    for (int i = 0; i < numRows; i++) {
        c.moveToPosition(i);//from   w w w. jav a  2 s.  c  o  m
        result.add(c.getString(DL_COL_PATH));
    }
    c.close();
    return result;
}

From source file:com.coinomi.wallet.ExchangeRatesProvider.java

public static List<ExchangeRate> getRates(final Context context, @Nonnull String localSymbol) {
    ImmutableList.Builder<ExchangeRate> builder = ImmutableList.builder();

    if (context != null) {
        final Uri uri = contentUriToCrypto(context.getPackageName(), localSymbol, true);
        final Cursor cursor = context.getContentResolver().query(uri, null, null, new String[] { null }, null);

        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();/*from  ww  w  .  j  a  va2 s.  co m*/
            do {
                builder.add(getExchangeRate(cursor));
            } while (cursor.moveToNext());
            cursor.close();
        }
    }

    return builder.build();
}

From source file:im.delight.android.commons.Social.java

/**
 * Returns a list of phone numbers for the contact with the given lookup ID
 *
 * @param contactLookupId a contact's lookup ID to get the phone numbers for
 * @param context a context reference/*from  w  w w.  j ava2s  .c om*/
 * @return CharSequence[] a list of all phone numbers for the given contact or `null`
 */
public static CharSequence[] getContactPhone(final String contactLookupId, final Context context) {
    final Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    final String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
    final String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    final String[] selectionArgs = new String[] { contactLookupId };
    final String sortOrder = null;

    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);

    String phone;
    if (result != null) {
        if (result.getCount() > 0) {
            final CharSequence[] res = new CharSequence[result.getCount()];

            int i = 0;
            while (result.moveToNext()) {
                phone = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                if (phone != null) {
                    res[i] = phone;
                    i++;
                }
            }

            result.close();

            return res;
        } else {
            result.close();

            return null;
        }
    } else {
        return null;
    }
}