5

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

rgreso
  • 496
  • 1
  • 7
  • 19

5 Answers5

2

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.

Community
  • 1
  • 1
Synxis
  • 9,236
  • 2
  • 42
  • 64
  • thanks for answer! yes it fixed that error but it doesnt insert selected contact to my editText and when I have this same code in Activity in another project it insert it – rgreso Feb 27 '14 at 12:25
  • Can you comment the `if(v==contactsButton)` (and its closing brace) to see if that works ? (Uh, why is UI in java so retarded ?) – Synxis Feb 27 '14 at 12:29
  • sorry I havent mentioned that there are another errors... It doesnt know RESULT_OK (in Activity it does), and it wants to implement managedQuery(In Activity it doesnt). It runs only if I remove if*(resultCode==RESULT_OK) and create unimplemented managedQuery method. Sorry I realised it now. I dont know how to implmenet that managedQuery method I really beginner and this code I find in some tutorial and test in Activity. – rgreso Feb 27 '14 at 12:35
  • Can you post all errors on you question please ? (without modifying their text). – Synxis Feb 27 '14 at 12:49
  • In your question, please. Not as a comment. – Synxis Feb 27 '14 at 12:54
  • Super thanks a lot! And have you any idea how I can fix the problem : The method managedQuery(Uri, null, null, null, null) is undefined for the type new View.OnClickListener(){}? thanks – rgreso Feb 27 '14 at 13:10
  • yes I have tried as it is shown in your link but as managesQuery also when I use getContentResolver a have an error : The method getContentResolver() is undefined for the type new View.OnClickListener(){} – rgreso Feb 27 '14 at 13:16
0

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

Akshay
  • 6,029
  • 7
  • 40
  • 59
  • yes I have that permision.. its writing to me that: void is an invalid type for the variable onActivityResult – rgreso Feb 27 '14 at 12:05
  • @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) return; 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); } } – Akshay Feb 28 '14 at 04:40
0

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.

Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • its impossible to run it it underlined onActivityResult and write: void is an invalid type for the variable onActivityResult – rgreso Feb 27 '14 at 12:08
  • You returnin your View v in the wrong place i.e. onActivityResult. It should be within the onCreateView method. – Atul O Holic Feb 27 '14 at 12:16
0

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

Udi Oshi
  • 6,787
  • 7
  • 47
  • 65
0
  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();
    }
Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35