我正在开发一个应用程序,我正在使用mapv2,为此我需要将五个位置保存到sharedpreferences中,因为我使用arraylist很好,后来我想在调用oncreate()方法时获取所有位置。
但是我的问题是假设我先保存2个位置并单击“后退”按钮,然后再次进入地图,它在地图上显示了我之前保存的2个位置,现在我保存了另外3个位置并单击“后退”按钮,然后再次去我的地图,现在它只显示最后3个位置,但我要总共5个位置。
在这里我发现了问题,即第二个3个位置数组被第一个2个位置数组覆盖,我试图解决此问题,但我没有得到。或者任何原因导致此问题,请告诉我。
我将放弃我的代码,接受任何类型的帮助。
提前致谢
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_or_delete_favourate_loc);
addressListView =(ListView)findViewById(R.id.listView);
clearButton = (Button)findViewById(R.id.clearBtn);
_googlemap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.favarouteLocMap)).getMap();
_googlemap.getUiSettings().setZoomControlsEnabled(true);
_googlemap.getUiSettings().setCompassEnabled(true);
_googlemap.getUiSettings().setMyLocationButtonEnabled(true);
hydLocation = new LatLng(17.3752800, 78.4744400);
_googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(hydLocation, 10));
_googlemap.setOnMapClickListener(this);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();
addressList = new ArrayList<String>(5);
distanceList = new ArrayList<String>();
latLngList = new ArrayList<LatLng>(5);
markerList = new ArrayList<Marker>();
latArray = new ArrayList<Float>();
lngArray = new ArrayList<Float>();
ArrayList<String> addAList=new ArrayList<String>();
int size1=sharedPreferences.getInt("size1",0);
int size2=sharedPreferences.getInt("size2",0);
int size3=sharedPreferences.getInt("size3",0);
for(int j=0;j<size1;j++)
{
addAList.add(sharedPreferences.getString("addr"+j,""));
}
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,addAList);
addressListView.setAdapter(adapter);
ArrayList<Float> latList = new ArrayList<Float>();
for(int j=0;j<size2;j++){
latList.add(sharedPreferences.getFloat("latArr"+j,0.0f));
}
Log.e("LAT", ""+latList.size());
ArrayList<Float> lngList = new ArrayList<Float>();
for(int j=0;j<size3;j++){
lngList.add(sharedPreferences.getFloat("lngArr"+j,0.0f));
}
Log.e("LNG", ""+lngList.size());
for(int i=0;i<latList.size();i++){
lat1=latList.get(i);
lng1=lngList.get(i);
Log.e("LAT LNG", ""+lat1+lng1);
latLng = new LatLng((double)lat1,(double)lng1);
getMarks(_googlemap, latLng);
}
}
@Override
public void onMapClick(final LatLng point) {
// TODO Auto-generated method stub
lat = point.latitude;
lng = point.longitude;
count=sharedPreferences.getInt("count",0);
count++;
editor.clear();
Geocoder geoCoder = new Geocoder(AddOrDeleteFavourateLocActivity.this, Locale.getDefault());
try{
List<Address> addresses = geoCoder.getFromLocation(point.latitude, point.longitude, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
final String strAdd = strReturnedAddress.toString();
alertDialogBuilder = new AlertDialog.Builder(
AddOrDeleteFavourateLocActivity.this);
alertDialogBuilder.setTitle("Enter Distance From Your Location");
input = new EditText(AddOrDeleteFavourateLocActivity.this);
input.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
alertDialogBuilder.setView(input);
alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
distance=input.getText().toString();
diskm ="\n"+distance+" km";
if(count==1){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker1);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
}
if(count==2){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker2);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
}
if(count==3){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker3);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
}
if(count==4){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker4);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
}
if(count==5){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker5);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
}
for(int i=0;i<addressList.size();i++)
{
editor.putString("addr"+i,addressList.get(i));
}
for(int i=0;i<latArray.size();i++)
{
editor.putFloat("latArr"+i,latArray.get(i));
}
for(int i=0;i<lngArray.size();i++)
{
editor.putFloat("lngArr"+i,lngArray.get(i));
}
editor.putInt("size1",addressList.size());
editor.putInt("size2",latArray.size());
editor.putInt("size3",lngArray.size());
editor.commit();
}
});
alertDialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
if(count==1){
marker1= getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count==2){
marker2=getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count==3){
marker3=getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count==4){
marker4=getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count==5){
marker5=getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count>5){
Toast.makeText(getApplicationContext(),"Sorry You Can Add Only Five Locations", 100).show();
}
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,addressList);
addressListView.setAdapter(adapter);
}
}
catch(Exception e){
e.printStackTrace();
}
}
private Marker getMarks(GoogleMap googleMap,LatLng arg0){
Marker marker;
MarkerOptions markerOption = new MarkerOptions();
markerOption.position(arg0).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)).flat(true);
markerOption.draggable(true);
marker=googleMap.addMarker(markerOption);
return marker;
}
public void clearData(View v){
_googlemap.clear();
editor.clear().commit();
adapter.clear();
}
最佳答案
ArrayList<String> addAList= null;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putParcelableArrayList("address", addAList);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
if(savedInstanceState!=null){
addAList = (ArrayList<String>) savedInstanceState.getSerializable("address");
}
else{
Toast msg = Toast.makeText(getApplicationContext(), "Values Not Restored!!!", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
}
}