Google Places AutoComplete Fragment将双Lat / Lon传递给Realm DB

时间:2016-12-06 15:46:32

标签: android realm google-places-api

我将谷歌自动完成片段的纬度和经度传递给Realm DB时遇到问题。我在文本视图中获取数据但我无法将此数据作为Double传递给Realm DB。 horse.setLocationLatitude设置为Double

  final LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
            R.layout.fragment_add_horse, container, false);


    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    autocompleteFragment.setHint("Enter Location");
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            mLocationNam.setText("Place: " + place.getName().toString());
            System.out.println("Place: " + place.getName());
            mLocationLat.setText("" + place.getLatLng().latitude);
            mLocationLon.setText("" + place.getLatLng().longitude);

        }

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

    return mLinearLayout;

}

    public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mLocationNam = (TextView) view.findViewById(R.id.loc_name);

    mLocationLat = (TextView) view.findViewById(R.id.loc_lat);
    mLocationLon = (TextView) view.findViewById(R.id.loc_lon);}

保存在领域

private void saveHorseInRealm() {

    getRealm().beginTransaction();
    Horse horse = getRealm().createObject(Horse.class);

    Long tsLong = System.currentTimeMillis()/1000;
    String ts = tsLong.toString();
    horse.setID(ts);

    horse.setName(mNameEditText.getText().toString());
    horse.setLocationName(mLocationNam.getText().toString());
    horse.setLocationLatitude(mLocationLat);
    horse.setLocationLongitude(mLocationLon);
           getRealm().commitTransaction();

    getActivity().onBackPressed();
}

1 个答案:

答案 0 :(得分:0)

horse.setLocationLatitude(Double.valueOf(mLocationLat.getText().toString()));
horse.setLocationLongitude(Double.valueOf(mLocationLon.getText().toString()));

虽然您应该从视图中抽象出您的数据using TextWatchers