可以从Fragment类

时间:2016-01-27 11:57:15

标签: java android-fragments autocomplete widget

我正在使用导航抽屉创建活动(扩展AppCompatActivity)以在多个片段之间切换,其中一个是使用MapView实现的MapFragment(扩展片段)。 我想在MapFragment类中调用Google提供的AutoComplete Widget。

这是我在MapFragment类中的代码

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

    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
            .build();
    autocompleteFragment.setFilter(typeFilter);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            String placeDetailsStr = place.getName() + "\n"
                    + place.getId() + "\n"
                    + place.getLatLng().toString() + "\n"
                    + place.getAddress() + "\n"
                    + place.getAttributions();
            Log.i("OnPlaceSelected", placeDetailsStr);
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15));

        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i("OnPlaceSelected", "An error occurred: " + status);
        }
    });

语法错误发生在

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

  

不可转换型;无法投出' android.support.v4.app.Fragment'到com.google.android.gms.location.places.ui.PlaceAutocompleteFragment'

但是当我把这组代码放在MainActivity中时,它运行得很好。 所以我想知道是否有任何方法可以在Fragment中调用AutoComplete Widget,它只是不能。

任何答案将不胜感激。 :)

5 个答案:

答案 0 :(得分:7)

使用getActivity()

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

答案 1 :(得分:4)

如果你试图从片段中找到片段,你必须使用这样的东西:

placeAutocompleteFragment = (PlaceAutocompleteFragment)getChildFragmentManager().findFragmentById(R.id.fragmentPlaces);

您可能会遇到 onPlaceSelected 未被调用的问题,这是因为您拥有的活动必须将结果转发到片段中。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    mapFragment.onActivityResult(requestCode, resultCode, data);
}

mapFragment 必须将结果转发到places片段:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    placeAutocompleteFragment.onActivityResult(requestCode, resultCode, data);
}

即便如此,我也遇到过没有调用 onPlacesSelected 的问题。通常最好不要嵌套地图并放置碎片,但只是在拥有活动中将它们分开。

答案 2 :(得分:1)

我一直在搜索interwebz并且无法找到解决方案,直到我意识到我与MapFragment有同样的问题,所以我应用了这种技术并且它有效!

片段:

AsyncTask

注意:我正在使用支持片段(android.support.v4.app.Fragment),这就是我使用SupportPlaceAutocompleteFragment

的原因

XML:

TypeError: jQuery(...).owlCarousel is not a function
jQuery('#spic-catlogues').owlCarousel({

答案 3 :(得分:1)

您使用SupportPlaceAutocompleteFragment代替PlaceAutocompleteFragment,在使用Fragment supportv4时将其替换

SupportPlaceAutocompleteFragment autocompleteFragment 
    = (SupportPlaceAutocompleteFragment) 
        getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

答案 4 :(得分:0)

我有同样的问题,感谢@Kalpesh的想法,它工作正常,没有eventListenner的问题,这是我的代码,如果它可以帮助某人。

我有一个带有viewpager和3个片段的tablayout,我们可以向右或向左滑动。

  

googlePLacesLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorFragment_bg">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="0dp"
    android:layout_marginBottom="0dp">
        <fragment
            android:id="@+id/autocomplete_fragment"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="80dp" />
</android.support.v7.widget.CardView>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/largeTexxt"
    android:layout_centerVertical="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>
  

MyFragmentClass.java

package com.raccoon.trash.aregood.TabFragments;
public class MyFragmentClass extends Fragment {

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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

final RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(
            R.layout.googlePLacesLayout, container, false);

    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    autocompleteFragment.setHint("Let's find some restaurants");

    final TextView textView = (TextView) mRelativeLayout.findViewById(R.id.largeTexxt);
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            textView.setText("Place: " + place.getName()+ place.getId()+
                    place.getAddress()+ place.getPhoneNumber()+ place.getWebsiteUri());
            System.out.println("Place: " + place.getName()+ place.getId()+
                    place.getAddress()+ place.getPhoneNumber()+ place.getWebsiteUri());
        }

        @Override
        public void onError(Status status) {
            System.out.println("An error occurred: " + status);
        }
    });

return mRelativeLayout;
}

}