Reputation: 2340
Working with android project and i got a problem on customized overlay class i cannot get the putextra values from the previous class to this class it shows an error on getIntent() method can any one help to fix this error here is my code..
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
/**
* Class used to place marker or any overlay items on Map
* */
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
String place_reference;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
// latitude
double lat = geopoint.getLatitudeE6() / 1E6;
// longitude
double lon = geopoint.getLongitudeE6() / 1E6;
//Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = getIntent();
i.getStringExtra(place_reference);
Intent intent = new Intent(context, SinglePlaceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent );
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
// MainActivity.this.finish();
}
});
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
and this is my error which i found on my code
Upvotes: 0
Views: 306
Reputation: 448
write like
Intent i= context.getIntent();
as your class holding this intent is an inner class.
Upvotes: 0
Reputation: 3846
I think it would be better to pass your place_reference
to the constructor and save it as an instance variable, instead of the whole activity. Obviuosly, this would be better when you only need one extra string, that too when it is used only once in this class. You can try:
String place_reference;
public AddItemizedOverlay(Drawable defaultMarker, String place_reference) {
this(defaultMarker);
this.place_reference = place_reference;
}
// and then use place_reference any where you need
Upvotes: 0
Reputation: 144
You need to pass the Activty to your AddItemizedOverlay. Then you can call
activity.getIntent();
Upvotes: 0
Reputation: 22064
getIntent()
is a method for Activity
.
In your case, I would make the constructor like this:
private Activity activity;
public AddItemizedOverlay(Drawable defaultMarker, Context context, Activity a) {
this(defaultMarker);
this.context = context;
this.activity = a;
}
And then use activity.getIntent();
Upvotes: 1