Using onActivityResult in fragments

Hi in the fragment I want to select a phone number from contacts and paste it into an EditText

but it doesn’t work in the fragment, I use it in action, and it works. Please, could you help me, how can I change this? thank

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;
    }

: RESULT_OK cannot be resolved by a variable

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

+4
source share
5 answers

Well, you have parentheses that are not located well. I suppose you want `onActivityResult to be in a 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 brackets and half-colony 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

: , . , ( K & R brace).

: Activity.RESULT_OK, raw RESULT_OK.

managedQuery: . .

getContentManager: . .

+2

, android.permission.READ_CONTACTS Permission .

0

.

  • statrtActivityForResults, ,

    getActivity().startActivityForResult(intent, 1);
    
  • super.onActivityResult(requestCode, resultCode, data) onActivityResult() @marcin_j.

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

Here is the link.

0
source

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

0
source
  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();
    }
0
source

Source: https://habr.com/ru/post/1529197/


All Articles