需要有关如何在适配器类上使用onActivityResult()的帮助?

时间:2019-05-27 16:45:14

标签: java android adapter onactivityresult

当我使用super.onActivityResult(requestCode, resultCode, data);时,我尝试从适配器类的联系人列表中返回电话号码。

btnContactGift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, 
ContactsContract.Contacts.CONTENT_URI);
                // Show only contacts with phone numbers

intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                // Start the Contacts activity
                context.startActivityForResult(intent, PICK_CONTACT);

            }
        });





public void onActivityResult(int requestCode, int resultCode, Intent data) {

 super.onActivityResult(requestCode, resultCode, data);


    switch (requestCode) {
        case PICK_CONTACT :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI};
Cursor c = conR.query(contactData, projection, null, null, null);
                c.moveToFirst();
int nameIdx =c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int phoneNumberIdx =c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int photoIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI);
                String name = c.getString(nameIdx);
                String phoneNumber = c.getString(phoneNumberIdx);
                String photo = c.getString(photoIdx);
                if (name == null) {
                    name = "No Name";
                }
                String nwPhone = phoneNumber.replace("+251", "0");

                edtPhoneGift.setText(nwPhone);
                c.close();

                // Now you have the phone number

            }
            break;
    }
 }
  

无法解析方法onActivityResult(int,int,Intent)

2 个答案:

答案 0 :(得分:1)

onActivityResult()需要在您调用startActivityForResult()的活动或片段上实现。对于您而言,这就是context(来自context.startActivityForResult(intent, PICK_CONTACT))标识的任何活动或片段。

答案 1 :(得分:0)

只需删除对超级super.onActivityResult(requestCode, resultCode, data)的调用,则不需要。

此外,您需要将投影中的ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI更改为其他内容,您可以从电话选择器意图返回的Photo.XXX中获得Uri字段,仅在Phone中的列即可。 XXX或暗中加入了它,您可以尝试改用Contacts.PHOTO_ID