0

I asked a question 1-2 days ago, it was answered. My codes are there. This is text link of my question

I added overlays on the map and when I click any overlay, I can get the id of it but there is a problem. I want to get the overlay's id and I want to send it to another class but, not via bundle.

I'm using fragments.

Here is my onTap function.

@Override
    public boolean onTap(int index) {
        OverlayItem item = mapOverlays.get(index);
        int id = Integer.valueOf(item.getSnippet());        
        Toast.makeText(context, "Ilan ID : " + id, Toast.LENGTH_LONG).show();

        MainActivity ma= new MainActivity();
        Param param = null;     
        param.setAdvertID(String.valueOf(id));


        // change page      
        ma.onTabChanged(TabEnum.ANASAYFA.toString());


       //Log.e("Tap", "Tap Performed"+id);
       return true;
    }

I want to send id parametres from ItemizedOverlay class-onTap function- to another class. How can I do it?

Edit : These are not working!, i couldn't do it. :(

    MainActivity ma= new MainActivity();
    Param param = null;     
    param.setAdvertID(String.valueOf(id));
    ma.onTabChanged(TabEnum.ANASAYFA.toString());

Thanks everyone.

Community
  • 1
  • 1
mertaydin
  • 2,184
  • 5
  • 19
  • 26

1 Answers1

1

If you are using fragments, you can provide a getter and setter in your activity, set de data in one fragment (to your activity) and get the data in your destination fragment with activity getter.

You can provide a static method to get the data from another activity, previous setting in your fragment.

I Think this is what you want.

Also, you can do it between fragments inside an activity with interfaces, see this for it.

Hope helps

Community
  • 1
  • 1
Francisco Hernandez
  • 2,378
  • 14
  • 18
  • onTabChanged method is in MainActivity i coudn't use it. And, Param class has setter and getter functions but when i define Param param; Eclipse says me, it must have initialize value that is must be null and when i use setAdvertID function it's giving me error. – mertaydin Dec 25 '12 at 13:30
  • Yes, that rights, but you can set de data before call onTabChanged in MainActivity, and in the destination fragment get the data from mainactivity. You are using Params, and initialice it with null, if you use it as you do, this is a NullPointerException. When you have fragments hold in and activity, the activity is the same object for all fragments, so you can use it to pass data between fragments – Francisco Hernandez Dec 25 '12 at 13:34
  • setAdvertId function sets the data, data is id. But it giving error. But if I use just this code : MainActivity ma= new MainActivity(); // change page ma.onTabChanged(TabEnum.ANASAYFA.toString()); nothing happens. – mertaydin Dec 25 '12 at 13:36
  • How can i use this param object? – mertaydin Dec 25 '12 at 13:41
  • 1
    You have to initialice it, this Param class is a custom class? do Param param = new Param();, if not, colud you gimme the full class name? (with package) – Francisco Hernandez Dec 25 '12 at 14:17
  • I created object with this way : Param param = new Param(); and it accepted the value but now the problem is that i can't reach the function that is in the MainActivity from ItemizedOverlay class. – mertaydin Dec 25 '12 at 14:23
  • I tried to reach MainActivity's method from BaseFragment. I created a method in BaseFragment and I get the MainActivity object in method to use MainActivity's method but it gave me NullPointerException. – mertaydin Dec 25 '12 at 14:41
  • In the fragment you can call 'getActivity()', this method is inerithed from super class, and will give you the activity object that hold the current fragment, then, with that object invoke your desired method – Francisco Hernandez Dec 26 '12 at 08:52