rgreso
rgreso

Reputation: 542

using onActivityResult in Fragments

Hi in fragment I want to pick a phone number from contacts and inset it into EditText

but its not working in fragment I use it in activity and it works. Please could you help me how I should change it< thanks

public class Encrypt extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.encrypt, null);  
        phoneNumberDisplay = (EditText) v.findViewById(R.id.editTextPhoneNumber);
        contactsButton=(Button)v.findViewById(R.id.buttonContacts);
        contactsButton.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {   
                if(v==contactsButton){
                    Intent intent=new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
                    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                    startActivityForResult(intent, 1);
                }
            }
        });

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                Uri ur = data.getData();
                Cursor c = managedQuery(ur, null, null, null, null);
                if (c.moveToFirst()) {
                    String s = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    phoneNumberDisplay.setText(s);
                }
            }
        }
        return v;
    }

errors: RESULT_OK cannot be resolved to a variable

The method managedQuery(Uri, null, null, null, null) is undefined for the type new View.OnClickListener(){}

Upvotes: 5

Views: 4607

Answers (5)

Med Elgarnaoui
Med Elgarnaoui

Reputation: 1667

  public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Activity ac =new Activity();
    Cursor cursor =ac.managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    // this is our fallback here
    return uri.getPath();
    }

Upvotes: 0

Synxis
Synxis

Reputation: 9388

Ok, you have a parenthesis that is not well placed. I suppose you want `onActivityResult to be in the click listener.

    contactsButton.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {   
            if(v==contactsButton){
                Intent intent=new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                startActivityForResult(intent, 1);
            }
        }
    });
//   ^^
// This parenthesis should not be here

Remove the parenthesis and the semi-colon, and add them here:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Uri ur = data.getData();
            Cursor c = managedQuery(ur, null, null, null, null);
            if (c.moveToFirst()) {
                String s = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phoneNumberDisplay.setText(s);
            }
        }
    });
//   ^^
//  Here, so that it is in the event listener

Note: since I fixed the indentation in your post I saw this error. Next time, try to always indent your code correctly, so that you will see that kind of errors (and that's also why I dislike K&R brace style).

Update: It is Activity.RESULT_OK, not a raw RESULT_OK.

For managedQuery: see this question.

For getContentManager: see here.

Upvotes: 2

Udi Oshi
Udi Oshi

Reputation: 6867

You are trying to return value when onActivityResult is void return function, you need to implement your logic without returning any value

Upvotes: 0

Atul O Holic
Atul O Holic

Reputation: 6792

There could be two possible reasons to this.

  1. Call your statrtActivityForResults using Activity's reference, like

    getActivity().startActivityForResult(intent, 1);
    
  2. Calling super.onActivityResult(requestCode, resultCode, data) in you onActivityResult() as @marcin_j also pointed.

Try either or both of them. See which one works for you.

Here's a link.

Upvotes: 0

Akshay
Akshay

Reputation: 6142

For accessing the contacts form your phone, make sure that you added the android.permission.READ_CONTACTS Permission in manifest file..

Upvotes: 0

Related Questions