简体   繁体   English

我如何获得使用 Google Place Autocomplete 的意图?

[英]how do I get my intent to work with Google Place Autocomplete?

Can you tell me what I need to do to make my intent work please?你能告诉我我需要做什么才能让我的意图起作用吗? When the user clicks mEditInit , the Places drop down menu appears.当用户单击mEditInit ,会出现 Places 下拉菜单。

When the user clicks the place in the menu, I want to send this back to mEditInit当用户单击菜单中的位置时,我想将其发送回mEditInit

I've used intents before but not working in this case.我以前使用过意图,但在这种情况下不起作用。

public class MyActivity extends AppCompatActivity  {

  String stuff;

  private EditText mEditInit;

  public static final int AUTOCOMPLETE_REQUEST_CODE = 1;


  @Override
  protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_my);

    mEditInit = (EditText) findViewById(R.id.edit_message);

    mEditInit.setX(0);
    mEditInit.setY(250);

    //Get the bundle
    Bundle bundle = getIntent().getExtras();


    if (stuff != null && !stuff.isEmpty())
    //Extract the data…
    { stuff = bundle.getString("stuff");
      mEditInit.setText(stuff);
    }

    Places.initialize(this, getString(R.string.places_api_key));

    mEditInit.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(MyActivity.this, "bowwow", Toast.LENGTH_SHORT).show();

        List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
        Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fields).build(MyActivity.this);
        startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
        //setResult(RESULT_OK, intent);

      }
    });

  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
      if (resultCode == RESULT_OK) {

        Place place = Autocomplete.getPlaceFromIntent(data);
        //Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();

        Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();

        Intent i = new Intent(this, MyActivity.class);

        String getrec= place.getName();

        //Create the bundle
        Bundle bundle = new Bundle();

        //Add your data to bundle
        bundle.putString("stuff", getrec);

        //Add the bundle to the intent
        i.putExtras(bundle);

        // Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
      } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
        // TODO: Handle the error.
        Status status = Autocomplete.getStatusFromIntent(data);
        // Log.i(TAG, status.getStatusMessage());
      } else if (resultCode == RESULT_CANCELED) {
        // The user canceled the operation.
      }
      //  super.onActivityResult(requestCode, resultCode, data);
    }
  }

}

In your onActivityResult method, you do not need any extra.在您的onActivityResult方法中,您不需要任何额外的东西。

Just call the method setText() on your EditText :只需在EditText上调用setText()方法:

@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
      if (resultCode == RESULT_OK) {

        Place place = Autocomplete.getPlaceFromIntent(data);
        //Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();

        mEditInit.setText(place.getName());
      } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
        // TODO: Handle the error.
        Status status = Autocomplete.getStatusFromIntent(data);
        // Log.i(TAG, status.getStatusMessage());
      } else if (resultCode == RESULT_CANCELED) {
        // The user canceled the operation.
      }
      //  super.onActivityResult(requestCode, resultCode, data);
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM