片段内的onSearch方法

时间:2016-05-24 14:40:09

标签: android search fragment

如何将此方法转换为片段类?我无法在片段(onCreate)中使用此方法。

 public void onSearch(View view) {
        EditText location_tf = (EditText) findViewById(R.id.TFaddress);
        String location = location_tf.getText().toString();
        List<Address> addressList = null;
        if (location != null || !location.equals("")) {
            Geocoder geocoder = new Geocoder(this);
            try {
                addressList = geocoder.getFromLocationName(location, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
                Address address = addressList.get(0);
            LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
            mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            }
    }

我不知道如何通过这个方法在onSearch中包含一个动作,布局已准备好接收方法但不知道如何在类中插入它。我试图在此代码中插入onSearch方法:

public class FragmentC  extends Fragment{
    MapView mMapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.frag4, container,
                false);
        mMapView = (MapView) v.findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        googleMap = mMapView.getMap();
        // latitude and longitude
        double latitude = 17.385044;
        double longitude = 78.486671;

        // create marker
        MarkerOptions marker = new MarkerOptions().position(
                new LatLng(latitude, longitude)).title("Hello Maps");

        // Changing marker icon
        marker.icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        googleMap.addMarker(marker);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));

        // Perform any camera updates here
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroyView();
       // Log.d(TAG, "onDestroyView");

        Fragment f = getActivity()
                .getSupportFragmentManager().findFragmentById(R.id.map);
        if (f != null) {
            getActivity().getSupportFragmentManager()
            .beginTransaction().remove(f).commit();
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

1 个答案:

答案 0 :(得分:0)

假设mMapView与mMap相同,并且有一个触发onSearch方法的按钮,这就是你的Fragment代码的样子。注意onCreateView的更改,并且onSearch方法没有收到View参数:

private EditText mLocationEditText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // inflat and return the layout
    View v = inflater.inflate(R.layout.frag4, container,
            false);
    mMapView = (MapView) v.findViewById(R.id.map);
    mMapView.onCreate(savedInstanceState);
    mMapView.onResume();// needed to get the map to display immediately

    // Obtain the EditText view reference first
    mLocationEditText = (EditText) v.findViewById(R.id.TFaddress);

    // Obtain the button that triggers the onSearch reference
    mButtonThatTriggersSearch = (Button) v..findViewById(R.id.search_button_id);
    // Set the onClickListener that should excecute when someome clicks the button
    mButtonThatTriggersSearch.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            onSearch();
        }
    });

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    googleMap = mMapView.getMap();
    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Hello Maps");

    // Changing marker icon
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

    // adding marker
    googleMap.addMarker(marker);
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
    googleMap.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));

    // Perform any camera updates here
    return v;
}

private void onSearch() {
    String location = mLocationEditText.getText().toString();
    List<Address> addressList = null;
    if (location != null || !location.equals("")) {
        Geocoder geocoder = new Geocoder(getActivity());
        try {
            addressList = geocoder.getFromLocationName(location, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
        mLocationEditText.addMarker(new MarkerOptions().position(latLng).title("Marker"));
        mLocationEditText.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    }
}