3

I want to pass some String arrays and size of that arrays in order to get location to show on a map activity.But the problem is I couldn't declare string arrays on the top of the onCreate method and it was declared in on create method since I used intent to pass variable.Therefore I couldn't use those arrays in the onMapReady method.

My codes are as follows,In my first activity

Intent SSMapIntent =new
Intent(SchoolSelectorMainActivity.this,MapsActivity.class);
        SSMapIntent.putExtra("maxDistance",Str_maxdistance);
        SSMapIntent.putExtra("source", 1);
        SSMapIntent.putExtra("listCount",listCount);
        if(listCount!=0){
            SSMapIntent.putExtra("nameList",nameListIntent);
            SSMapIntent.putExtra("typeList", typeListIntent);
            SSMapIntent.putExtra("distanceList",distanceListIntent);
            SSMapIntent.putExtra("latitude",latListIntent);
            SSMapIntent.putExtra("longitude",lngListIntent);
        } 
        startActivity(SSMapIntent);

My map activity

public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {

private GoogleMap mMap;
int source,tempInt,listCount;
String message1="",message2="",message3="",message4="";
String [][] selectedListTxt = new String[listCount][3];//for get name,lat,lng
double maxDistance;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Intent intent = this.getIntent();
    Bundle bundle = intent.getExtras();
    source=bundle.getInt("source");
    maxDistance=bundle.getDouble("maxDistance");
    if (source==1){
        listCount=bundle.getInt("listCount");

    }
    if (listCount!=0) {
        String[] nameListMap = bundle.getStringArray("nameList");
        String[] typeListMap = bundle.getStringArray("typeList");
        String[] distanceListMap= bundle.getStringArray("distanceList");
        double [] latListMap=bundle.getDoubleArray("latitude");
        double [] lngListMap=bundle.getDoubleArray("longitude");
    }
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //Here i can't use nameListMap,latListMap,lngListMap
      }
    }
Amitsharma
  • 1,577
  • 1
  • 17
  • 29

3 Answers3

0

You could use different approaches for your case. It depends on not only current situation but future probable changes as well, how complex data could be. For example, you can create parceable object where you put your strings, doubles, etc or create json from your data and send it like string. You could see more information about it How to send an object from one Android Activity to another using Intents?

Community
  • 1
  • 1
Mk.Sl.
  • 2,879
  • 1
  • 11
  • 11
0

Create a Util class (it is better to declare this class as signleton) and declare these arrays in there with public keyword and use these arrays in your MapsActivity class.
Don't forget to assign them null when you finished your job.
But I couldn't understend why you could not declare string arrays on the top of the onCreate method.

ondermerol
  • 524
  • 1
  • 11
  • 29
  • i can declare string arrays at the onCreate method. then it wont be able to used in on map ready method.therefor i need to declare string arry before onCreate method.can you give me any example to understand how can i solve my issue as you mentioned. – Kasun Sampath Jan 06 '16 at 08:14
0
    private GoogleMap mMap;
    int source,tempInt,listCount;
    String message1="",message2="",message3="",message4="";
    String [][] selectedListTxt = new String[listCount][3];//for get name,lat,lng
    double maxDistance;
    //global definition of required changes   
    String[] nameListMap=null;
    String[] typeListMap =null;
    String[] distanceListMap=null;
    double [] latListMap=null;
      double [] lngListMap=null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        source=bundle.getInt("source");
        maxDistance=bundle.getDouble("maxDistance");
        if (source==1){
            listCount=bundle.getInt("listCount");

        }
        if (listCount!=0) {
            nameListMap = bundle.getStringArray("nameList");
             typeListMap = bundle.getStringArray("typeList");
            distanceListMap= bundle.getStringArray("distanceList");
             latListMap=bundle.getDoubleArray("latitude");
             lngListMap=bundle.getDoubleArray("longitude");
        }
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        //Here i can't use nameListMap,latListMap,lngListMap
        // now you can use your variables here
        //SYSO(nameListMap.length)
          }
        }

Please see the changes i have made in your code ... i decare your required Varibale Globally...

koutuk
  • 832
  • 1
  • 8
  • 17