views:

115

answers:

2

Hey there,

Ok so this is what i've got.

Button anandabutton = (Button) findViewById(R.id.anandaAddressButton);
anandabutton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(),MapClass.class);
        startActivityForResult(myIntent,0);

    }

});

This method opens up my MapClass class which at the moment I just have set to show the location of one place.

But I have a load of buttons and rather than making a lot of different mapClass classes for each button, I am wondering can I just use the one class and depending on what button 'id' is pressed, it will check an 'if statement' and then put in the correct coordinates into the method to display the map. It would be a lot neater than coding up like 20-30 classes.

I'm not sure if i've explained that right so if not let me know.

Thanks for any help.

This is my map class now...

public class MapClass extends MapActivity {

MapView mapView;
MapController mc;
GeoPoint p;

@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    int button = getIntent().getExtras().getInt("button", -1);
    switch(button){
    case DundrumSelector.BUTTON1:
        handleCoordinates("53.288719","-6.241179");
        break;
    case DundrumSelector.BUTTON2:
        handleCoordinates("53.288719","-6.241179");
        break;
    }
}




private void handleCoordinates(String l, String b){


    mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
    View zoomView = mapView.getZoomControls();

    zoomLayout.addView(zoomView,
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

    mc = mapView.getController();
    String coordinates[] = {l, b};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
            (int) (lat*1E6),
            (int) (lng*1E6));

    mc.animateTo(p);
    mc.setZoom(17);
    mapView.invalidate();


}

and then my other activity is

public static final int BUTTON1 = R.id.anandaAddressButton;

Button anandabutton = (Button) findViewById(R.id.anandaAddressButton);
anandabutton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(),MapClass.class);
        myIntent.putExtra("button", BUTTON1);
        startActivityForResult(myIntent,0);

    }

});

For some reason when I try and click on any other item in the list other than BUTTON1 the program crashes. Ive had to take out BUTTON2, BUTTON3 etc because it just keeps crashing if i try and go into their view. but when i comment out this onClick method they all work fine?

Do either of you guys have any openions? Do u understand my problem? Thanks.

+1  A: 

If I understand you correctly, you want to always start the same Intent, but on the receiver side on the intent, that is the activity that is started, you want to use different methods depending on which button has been pressed.

I would suggest to add a constant to the Intent depending on the button you pressed, and the in the MapClass you check which constant it is and you react according to do that:

public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(),MapClass.class);
        myIntent.putExtra("button", BUTTON1);
        startActivityForResult(myIntent,0);
 }

BUTTON1 and BUTTON2 are constant int which you have do define in your Acitivty class correspondig to the buttons you want to add.

In the MapClass in the onCreate() method you do the following:

int button = getIntent().getExtras().getIntExtra("button", -1);
switch(button) {
   case BUTTON1:
   //do something
   break;
   case BUTTON2:
   //do method for button 2
   break;
   default:
   //do another thing
   break;
}

To also cover your edit:

if only the coordinates change the i would do something linke this:

int button = getIntent().getExtras().getIntExtra("button", -1);
switch(button) {
   case BUTTON1:
   handleCoordinates("53.288719","-6.241179");
   break;
   case BUTTON2:
  handleCoordinates("23.288719","23.241179"
   break;
   default:
   //do another thing
   break;
}



private void handleCoordinates(String l, String b) {
mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
    View zoomView = mapView.getZoomControls();

    zoomLayout.addView(zoomView,
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

    mc = mapView.getController();
    String coordinates[] = {l, b};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
            (int) (lat*1E6),
            (int) (lng*1E6));

    mc.animateTo(p);
    mc.setZoom(17);
    mapView.invalidate();
}
Roflcoptr
This is nice but it won't work if the user presses the back button when MapClass is loaded and then presses another button, this second press won't call MapClass.onCreate(), therefore displaying the coordinates that were last displayed before pressing back.
Ricardo Villamil
ok thanks yeah you've understood me correctly. i'm just working my through it now. do i have to define BUTTON1 BUTTON2 etc in the MapClass as well as the other activity class?
Capsud
i would just define it in the other activity class as a public constant. then you can access it by OtherClass.BUTTON1@ricardo: hmm although he startsActivityForResult()?
Roflcoptr
ok thanks yeah i got ya.Ok one other thing...rather than copying out the long method that i'll be calling when each button is pressed, is there a shorter way i can put in under each 'case', like just the coordinates change or something like that?Because if i copy in the whole method under each button case the class will get huge!thanks guys.
Capsud
hmm it's difficult to judge without any code. but try to putt all common code in one method and the part of the code which is different for each button also put in separate methods
Roflcoptr
how do i paste code here so it shows up grey like above?
Capsud
@sebi: yeah, cuz when you press the back button it calls onPause(), when the intent gets fired again, Android sees the Activity is still hot, it doesn't need to create it again, it just calls onResume() (unless the system ran out of memory and killed it). To me this is a design flaw in Android...
Ricardo Villamil
@capsud: edit your question. in the comments this isn't possible@ricardo: hmm then he should override onNewIntent()?
Roflcoptr
hey guys ive edited my question above to add in my mapClass. as i said i'm just wondering do I have to add in that whole long method for each case, or can i shorten it down a bit.
Capsud
i also edited my answer
Roflcoptr
+1  A: 

To complement on Sebi's answer and the question you asked in the comments, what you want to do is create one handler (could be in your onCreate() method):

OnClickListener buttonListener = new OnClickListener(){
    public void onClick(View v) {
        if(!(v instanceof Button))
           //throw error, not supposed to happen
        Button b = (Button)v;
        int id = b.getId();
        int code; //used to know when your activity comes back (if you need to)
        int lat;
        int long;
        switch(id) {
           case button1.getId():
              //set code, lat and long for button1
           case button2.getId():
              //set code, lat and long for button2
        ...
        }
        Bundle bdl = new Bundle();
        bdl.putDouble("latitude", lat);
        bdl.putDouble("longitude", long);
        Intent myIntent = new Intent(view.getContext(),MapClass.class);
        myIntent.putExtras(bdl);
        startActivityForResult(myIntent,code);
}
button1.setOnClickListener(buttonListener);
button2.setOnClickListener(buttonListener);
...
Ricardo Villamil
thanks. btw. if you test instanceof, it isn't necessary to test null
Roflcoptr
cool, didn't know, edited my answer ;)
Ricardo Villamil
you guys have been great thanks alot. i'll get back here later if i have any trouble once i get a chance to implement it! :)
Capsud
i'm having some trouble again and ive edited my first post. Any help appreciated. thanks alot.
Capsud
Take a look at how you pass the extras to your map activity. See my code on how to create a Bundle and add to the intent.
Ricardo Villamil