我想传递一些String数组和该数组的大小,以便在地图活动上显示位置。但问题是我无法在onCreate方法的顶部声明字符串数组并且它是在因为我使用intent来传递变量,所以在create方法上。因此我无法在onMapReady方法中使用这些数组。
我的代码如下,在我的第一个活动中
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);
我的地图活动
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
}
}
答案 0 :(得分:0)
您可以针对您的情况使用不同的方法。它不仅取决于当前的情况,还取决于未来可能的变化,数据的复杂程度。例如,您可以创建parceable对象,您可以在其中放置字符串,双精度等,或者从数据中创建json并将其作为字符串发送。您可以看到有关它的更多信息How to send an object from one Android Activity to another using Intents?
答案 1 :(得分:0)
创建一个Util类(最好将此类声明为signleton)并使用public关键字声明这些数组,并在 MapsActivity 类中使用这些数组。
完成工作后不要忘记将它们赋值为null。但我无法解释为什么你不能在onCreate方法的顶部声明字符串数组。
答案 2 :(得分: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)
}
}
请查看我在您的代码中所做的更改...我在全球范围内确认您所需的Varibale ......