不能在overlay类中使用putExtra方法

时间:2012-10-18 12:03:46

标签: android android-intent itemizedoverlay

这是我新的android程序,它是一个自定义的覆盖类我不能使用putExtra()将参考值发送到下一个活动我可以成功运行这个项目把它不会发送任何值到下一个类的方法可以任何人帮我解决这个错误

import java.util.ArrayList;
import android.app.Activity;
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;
       private Activity activity;
       String 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();
       }

       public AddItemizedOverlay(Drawable defaultMarker, Context context, Activity a) {
            this(defaultMarker);
            this.context = context;
            this.activity = a;
        }

       @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());
         reference = item.getSnippet();
         dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

//              Intent a = activity.getIntent();
//      String  reference=a.getStringExtra(place_reference);
                System.out.println("this is additemized :"+reference);
                Intent intent = new Intent(context, SinglePlaceActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("KEY_REFERENCE",reference);
                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();
       }

    }

1 个答案:

答案 0 :(得分:3)

开始活动之前

SharedPreferences prefs = context.getSharedPreferences("myprefs", 0);
SharedPreferences.Editor editor =prefs.edit();
editor.putString("KEY_REFERENCE", reference);
editor.commit();

现在开始您的活动后,您可以将其作为

进行访问
SharedPreferences prefs = getSharedPreferences("myprefs", 0);
String reference=  prefs.getString("KEY_REFERENCE","");