多选Android联系人和保存在ArrayList中

时间:2014-11-29 07:03:07

标签: arraylist android-contacts multi-select

我想将选定的本机联系人保存到SQLite数据库,能够从onActivityResult方法获取联系人,但我想多选联系人并将其存储在ArrayList中。这是我的代码。请让我知道解决问题的可能方法。

 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

        startActivityForResult(intent, PICK_CONTACT);

        Cursor cursor = getContentResolver().query(   ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
        while (cursor.moveToNext()) {
            String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      }

onActivityResult方法在下面给出

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     String pathToOurFile = Environment.getExternalStorageDirectory().getPath() + "/dcim/camera/" + imgName;
     new ImageUploadTask().execute();
     switch(requestCode){
        case 1: {       

           if (resultCode == Activity.RESULT_OK)
           {
               Uri contactData = data.getData();
               Cursor c = managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst())
                {
                    String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (hasPhone.equalsIgnoreCase("1")) 
                    {
                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                               ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                        phones.moveToFirst();
                        String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();

                        String nameContact = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        Log.d("Contact Testing ", nameContact);
                        Toast.makeText(getApplicationContext(), nameContact, Toast.LENGTH_SHORT).show();
                        //editText.setText(nameContact+ " "+ cNumber);
                    }
               }
         }
        }
    }
    }

0 个答案:

没有答案