android - Google 地方信息自动完成

标签 android android-layout android-fragments

我正在尝试添加 google place 自动完成功能,它工作得很好,但每次我点击它来搜索我的位置时,我的键盘都会显示,然后立即消失

代码xml

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <fragment
            android:id="@+id/place_autocomplete_fragment"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

java代码

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

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            location = place.getName().toString();

            FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
            String user_id = current_user.getUid();
            mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);

            HashMap<String, String> userMap = new HashMap<>();
            userMap.put("User location", location);


            mDatabase.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {

                    if(task.isSuccessful()){

                        Intent addlocation = new Intent(selectlocation.this, home.class);
                        addlocation.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(addlocation);
                        finish();


                    }

                }
            });
        }
        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
        }
    });

而且我还没有测试过,但是如果正确使用它,请帮助我检查 java 代码。我正在尝试将用户位置发送到我的 firbase 数据库,我不知道该代码是否正确,因为我还没有测试过它

最佳答案

我遇到了同样的问题。确保在 Developer Console 中启用适用于 Android 的 Google Places API 而不仅仅是 Places API . “Places API for Android”不会出现在 APIs & Auth/APIs 下,因为它还不是一个流行的 API(目前)。您必须使用 API 搜索框进行搜索。

关于android - Google 地方信息自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52154839/

相关文章:

android - ionic 2 : Add platform doesn't work

Android:InflateException:二进制 XML 文件行:膨胀类 <unknown> 时出错

android - 无法让 DialogFragment 以编程方式关闭

android - 启动时无法在 Android 应用程序中实例化 fragment

android - 如何在输入文本时更改文本编辑控件的颜色并在输入完成后再次更改颜色

android - :app:compileDebugJava Failed at Android Studio IDE

java - 在 Android 中使用 Weka 将 CSV 转换为 ARFF 时出错

java - android-想要将 Button 放在 listview 之上

android - findViewById 在膨胀后在自定义 View 中返回 null

android - 导航 fragment 更改时消失的工厂图标