I have a mapview, with itemizedoverlays, exactly like in the example of android developers guide: http://developer.android.com/resources/tutorials/views/hello-mapview.html
on the itemizedoverlay, i have a personalized dialog, with a button. all fine until here, but now i have problems trying to add the functionality to the button. I need that the button start's a new activity, but i can't achieve that.... ¿why? because on this line: i = new Intent (NyItemizedOverlay.class, Locate.class);
i have the current Intent class as first parameter, and the target intent class at second parameter.
MyItemizedOverlay is not a Intent class... it's ItemizedOverlay extension, then it doesn't compile when i try to launch the intent, i am trying to pass a normal class as first argument, and it needs an intent class. I have to put the launcher class, but the launcher class is not an intent :S
If i try to put another intent class on the first argument, i got this error: No enclosing instance of the type AllActivity is accessible in scope
.... (AllActivity is a public activity class of my app)
How i can solve this?
full code here:
public class MyItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private ArrayList<String> permissions = new ArrayList<String>();
private Context mContext;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void addOverlay(OverlayItem overlay,String permission) {
mOverlays.add(overlay);
permissions.add(permission);
populate();
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
//super(defaultMarker);
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void clear()
{
mOverlays.clear();
permissions.clear();//lista de permisos de cada usuario, ya que hay dos campos, el email (snippet) y el permission, una lista.
}
protected boolean onTap(int index) {
try{
OverlayItem item = mOverlays.get(index);
if (permissions.size()==0)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
}
else
{
//set up dialog
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.personal_dialog);
dialog.setTitle(item.getTitle());
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up text
TextView DialogEmail = (TextView) dialog.findViewById(R.id.DialogEmail);
TextView DialogPermission = (TextView) dialog.findViewById(R.id.DialogPermission);
DialogEmail.setText(item.getSnippet());
DialogPermission.setText(permissions.get(index));
final String userName=item.getTitle();
fi开发者_如何学JAVAnal String email=item.getSnippet();
final int cont=index;
//set up button
Button button = (Button) dialog.findViewById(R.id.DialogButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do something
Bundle bundle = new Bundle(); //bundle is like the letter
bundle.putString ("user",userName ); //arg1 is the keyword of the txt, arg2 is the txt
bundle.putString ("email", email);
bundle.putString ("permission", permissions.get(cont));
Intent i=null;
i = new Intent (MyItemizedOverlay.class, Locate.class);
i.putExtras(bundle);
startActivity(i);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
}catch(Exception e){}
return true;
}
}
The Intent javadoc clearly show that the first arg has to be a Context (extended by activity) and the second one the class of the activity you're trying to launch. In your case, you'll need to do :
Intent intent = new Intent(mContext, AllActivity.class);
mContext.startActivity(intent);
generally, Intent gets in one of its constructor Context
as first parameter and an Activity
class as the second one. So, do the following:
Intent intent = new Intent(mContext, AllActivity.class);
mContext.startActivity(intent);
To an intent constructor it should pass A Context
of the application package implementing this class, check this. it is not the intent.
It must be some Activity, say MyClass
that using your MyItemizedOverlay class. Best thing to do is declare a static
Context
say myContext
there and declare its value as myContext = MyClass.this;
in onCreate
. Then you can declare intent like this
new Intent (MyClass.myContext , Locate.class);
精彩评论