将搜索字符串额外传递给Google自动完成功能

时间:2018-10-23 06:37:06

标签: android googleplacesautocomplete

我有一个活动,然后去autocomplete activity。我想将上一个活动中的数据作为String extra发送到Google自动填充,并在搜索栏上将其显示为查询字符串。这可能吗?

这是我的代码

Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build(this);
startActivityForResult(intent, REQUEST_GOOGLE_PLACE_AUTOCOMPLETE);

enter image description here

3 个答案:

答案 0 :(得分:0)

解决方案:

是的,有可能。请按照以下步骤操作:

第1步:使用intent.putExtra(..)将您的搜索字符串从一项活动发送到另一项活动(就像往常一样)

第二步: [一如往常]使用getStringExtra(..)接收下一个活动中的字符串

步骤3:onCreate()之前声明全局对象:

public PlaceAutocompleteFragment autocompleteFragment;

第4步:对其进行初始化:

autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

步骤5:接下来,将搜索栏投射到EditText并将您的字符串设置为文本,如下所示:

((EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setText(your_string); [from previous activity]

注意: 请勿更改此行getView().findViewById(R.id.place_autocomplete_search_input),它必须与此解决方案中显示的相同。

就是这样。希望对您有帮助。如有任何问题,请发表评论。

答案 1 :(得分:0)

到目前为止,我还没有收到解决方案,因此我尝试检查PlaceAutocomplete.class并找到了我一直在寻找的解决方案。

通过查看

public final PlaceAutocomplete.IntentBuilder zzh(@Nullable String var1) {
        if (var1 != null) {
            this.intent.putExtra("initial_query", var1);
        } else {
            this.intent.removeExtra("initial_query");
        }

        return this;
    }

并致电Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).zzh("dasdas").build(this);

解决了问题。 保存此内容以供将来参考

答案 2 :(得分:0)

我正在使用软件包中的Autocomplete

  

com.google.android.libraries.places.widget

自动完成功能具有方法setInitialQuery()

我将通过传递查询字符串和latlng参数作为以下内容来开始活动:

private void startAutocompleteActivity(String query) {
        List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG);
        Intent intent = new Autocomplete.IntentBuilder(
                AutocompleteActivityMode.FULLSCREEN, fields)
                .setTypeFilter(TypeFilter.ADDRESS)
                .setInitialQuery(query)
                .setLocationBias(RectangularBounds.newInstance(new LatLng(10.637859, 76.691510), new LatLng(11.345122, 77.324748)))
                .build(this);
        startActivityForResult(intent, REQUEST_SOURCE_LOCATION);
    }