2016-05-24 5 views
0

Wie kann ich diese Methode in einer Fragmentklasse verwenden? Ich kann diese Methode nicht in einem Fragment (onCreate) verwenden.onSearch-Methode innerhalb des Fragments

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)); 
      } 
    } 

Ich weiß nicht, wie die Aufnahme einer Aktion durch diese Methode onSearch, das Layout ist bereit, das Verfahren zu erhalten, aber nicht wissen, wie es in der Klasse einzufügen. Ich versuche, die onSearch Methode in diesem Code einzufügen:

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

Sie sollten mehr Kontext zu machen, weil es scheint, dass Sie dies in Ihrer Aktivität erklärt haben, aber es ist nicht klar, warum Sie wollen würde, Das sollst du aus deinem Fragment nennen, was machst du in deinem Fragment und was genau willst du damit? –

+1

Danke für die Hilfe, ich habe die Beschreibung bearbeitet, bitte schauen Sie noch einmal. @azetaguionbajo –

+0

Dort habe ich die Antwort bearbeitet. Das sollte es tun. –

Antwort

0

Unter der Annahme, dass mMapView die gleichen wie MMAP ist, und dass Theres eine Schaltfläche, die die onSearch Methode auslöst, ist hier, wie Ihr Fragment Code aussehen sollte. Achten Sie auf die onCreateView Änderungen und dass die Methode onSearch keine Ansicht param nicht recieves:

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)); 
    } 
}