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();
}
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? –
Danke für die Hilfe, ich habe die Beschreibung bearbeitet, bitte schauen Sie noch einmal. @azetaguionbajo –
Dort habe ich die Antwort bearbeitet. Das sollte es tun. –