今天热门
热点:

android,出了点小问题,android,新手问题


安卓 新手 public class Utils {
    private static final String TAG = "UTILS";

    /**
     * 输出Cursor结果集
     *
     * @param cursor
     */
    public static void printCursor(Cursor cursor) {
        if (cursor != null && cursor.getCount() > 0) {

            int columnCount;
            String columnName;
            String columnValue;   //列值

            while (cursor.moveToNext()) {
                // 获得行中所有的列的总数
                columnCount = cursor.getColumnCount();

                for (int i = 0; i < columnCount; i++) {
                    columnName = cursor.getColumnName(i);
                    columnValue = cursor.getString(i);
                    Log.i(TAG, "当前是第" + cursor.getPosition() + "行: " + columnName + " = " + columnValue);
                }
            }
            cursor.moveToPosition(-1); //结束循环
// cursor.close();
        }
    }

    //根据号码取得联系人的名字
    public static String getContactName(ContentResolver contentResolver, String address) {
        Uri uri = new Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
        Cursor cursor = contentResolver.query(uri, new String[]{Phone.DISPLAY_NAME}, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            String cotractName = cursor.getString(0);
            cursor.close();
            return cotractName;
        }
        return null;
    }

    //给定uri找到查询人的id
    public static int getContractID(ContentResolver contentResolver, Uri uri) {
        Cursor cursor = contentResolver.query(uri, new String[]{"has_phone_number", "_id"}, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int hasPhoneNumber = cursor.getInt(0);
            if (hasPhoneNumber > 0) {
                int contactID = cursor.getInt(1);
                cursor.close();
                return contactID;
            }

        }
        return -1;  //-1表示返回空
    }
    //根据联系人id取得联系人的手机号
    public static  String getContactAddress(ContentResolver contentResolver,int contact_id){
        String selection ="contact_id=?";
        String[] sectionArgs = {String.valueOf(contact_id)};
        Cursor cursor =contentResolver.query(Phone.CONTENT_URI,new String[]{Phone.NUMBER},selection,sectionArgs,null);
        if(cursor!=null &&cursor.moveToFirst()){
            String address =cursor.getString(0);
            cursor.close();
            return address;
        }
        return null;
    }

解决方案

Uri uri = new Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));

把上面那句中new刪去看看

SDK版本改高一些

www.zrccd.nettrue/topics/20180309/188731.htmlTechArticleandroid,出了点小问题,android,新手问题 安卓 新手 publicclassUtils{ privatestaticfinalStringTAG="UTILS"; /** *输出Cursor结果集 * *@paramcursor */ publicstaticvoidprintCursor(Cursorcursor){ if(cursor!=nullcursor.getCount()...

相关文章

    暂无相关文章

用户评论

大家都在看