在Android中获取并选择联系人

时间:2015-07-15 06:17:00

标签: android android-studio contacts

我尝试获取所有联系人并选择它。

我完成了手机中的所有联系人。但是,当我尝试选择几个联系人并获取他们的名字或号码时,我遇到了零点错误。

    <script type="text/javascript">
    $(document).ready(function() {
        var track_load = 0; //total loaded record group(s)
        var loading  = false; //to prevents multipal ajax loads
        var total_groups = <?php echo $total_groups; ?>; //total record group(s)

        $('#results').load("autoload_process.php", {'group_no':track_load}, function() {
 //track_load++; <------ remove this from the code
}); //load first group

        $(window).scroll(function() { //detect page scroll

            if($(window).scrollTop() + $(window).height() > $(document).height()-200)  //user scrolled to bottom of the page?
            {

                if(track_load <= total_groups && loading==false) //there's more data to load
                {
                    loading = true; //prevent further ajax loading
                    $('.animation_image').show(); //show loading image

                    //load data from the server using a HTTP POST request
                    // add per-increment to ++track_load
                    $.post('autoload_process.php',{'group_no': ++track_load}, function(data){

                        $("#results").append(data); //append received data into the element

                        //hide loading image
                        $('.animation_image').hide(); //hide loading image once data is received

                        //track_load++; <------ remove this from the code
                        loading = false; 

                    }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?

                        alert(thrownError); //alert with HTTP error
                        $('.animation_image').hide(); //hide loading image
                        loading = false;

                    });

                }
            }
        });
    });
</script>

我认为问题是我的onItemClickListener。

我该如何解决这个问题? 谢谢你们:)

1 个答案:

答案 0 :(得分:1)

主要问题是进行查询时<xacml-ctx:Request ReturnPolicyIdList="true" CombinedDecision="false" xmlns:xacml-ctx="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17"> <xacml-ctx:Attributes Category="http://axiomatics.com/xacml/attribute-category/none" > </xacml-ctx:Attributes> <xacml-ctx:Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:environment" > </xacml-ctx:Attributes> <xacml-ctx:Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" > </xacml-ctx:Attributes> <xacml-ctx:Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:action" > </xacml-ctx:Attributes> <xacml-ctx:Attributes Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" > <xacml-ctx:Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:subject:subject-id" IncludeInResult="true"> <xacml-ctx:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">PI</xacml-ctx:AttributeValue> </xacml-ctx:Attribute> </xacml-ctx:Attributes> </xacml-ctx:Request> 为空。

另一个问题是您只使用uriContact作为投影,因此ID是唯一返回的内容。

我使用ContactsContract.Contacts._ID进行投影,以便返回所有行。

我还添加了查找当前所选联系人的功能,并显示带有电话号码的Toast。

这不是最佳代码,因为它只查询所有行,然后遍历它们,直到它找到当前选定的联系人,但它有效:

null

编辑:我有一个更优化的版本,它使用查询中的selection和selectionArgs返回当前的联系信息:

  getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cur = mAdapter.getCursor();
            cur.moveToPosition(position);
            String curName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
            System.out.println(curName);
            uriContact = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            Cursor cursorID = getContentResolver().query(uriContact,
                    null,
                    null, null, null);
            for (cursorID.moveToFirst(); !cursorID.isAfterLast(); cursorID.moveToNext() ) {
                String testName = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
                if (testName != null && testName.equals(curName)) {
                    contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
            }
            if (contactID != null) {
                System.out.println(contactID);
                Toast.makeText(MainActivity.this, "contact Phone: " + contactID, Toast.LENGTH_LONG).show();
            }
        }
    });