Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(){}

like image 805
rgreso Avatar asked Nov 02 '22 04:11

rgreso


1 Answers

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.

like image 94
Synxis Avatar answered Nov 15 '22 03:11

Synxis