我在ContactPicker中重复了一些数字

时间:2012-12-11 21:40:06

标签: android android-contacts

我在Android中使用ContactPicker时遇到问题。我的代码是下一个

public void showContacts(View v){
        phoneNumber.setText("");

        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);  
        startActivityForResult(contactPickerIntent, 1001); 
    }

    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
                case 1001:
                    Cursor cursor = null;  
                    String[] numeros = null;
                    try {  
                        Uri result = data.getData();
                        String id = result.getLastPathSegment(); 
                        cursor = getContentResolver().query(Phone.CONTENT_URI, 
                                null, 
                                Phone.CONTACT_ID + "=?", new String[] {id}, null);

                        int phoneIdx = cursor.getColumnIndex(Phone.NUMBER);
                        if(cursor.getCount() > 0){
                            numeros = new String[cursor.getCount()];
                            if (cursor.moveToFirst()) {
                                int cont = 0;
                                do{
                                    numeros[cont] = cursor.getString(phoneIdx);
                                    cont++;
                                }while(cursor.moveToNext());
                            }
                        }
                    } catch (Exception e) {  
                        Log.e("Exception", "Failed to get email data", e);  
                    } finally {  
                        if (cursor != null) {  
                            cursor.close();  
                        }
                        if(numeros != null){
                            if(numeros.length > 1){
                                showNumbers(numeros);
                            }else{
                                phoneNumber.setText(numeros[0]);
                            }
                        }
                    } 
                break;
            }
        }
    }

    private void showNumbers(final String[] numeros){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setSingleChoiceItems(numeros, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogo, int item) {
                phoneNumber.setText(numeros[item]);
                dialogo.cancel();
            }
        });

        builder.create().show();
    }

基本上在我调用ContactPicker的第一种方法中,在第二种方法中我收到了这个答案,我处理并验证用户是否有多个电话。

问题是,算法将数字返回六倍。我不知道为什么会这样。

0 个答案:

没有答案