Question

I am developing one application in that ,i am working with mapv2, in this i need to save five locations into sharedpreferences for that i used arraylist its fine, and later i want to get all the locations when ever oncreate() method called.

But my problem is suppose first i save 2 locations and click on back button and again i am going to my map,it shows 2 location on map what i saved before,now i save another 3 locations and click on back button and again i am going to my map,now it is displaying last 3 locations only but i want total 5 locations.

here i found on problem i.e., second 3 locations array override by the first 2 locations array,i trying to solve this problem but i didn't get.or any reason for this problem please let me know. i will drop my code,any type of help accepted . thanks in advance

@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();
}
Was it helpful?

Solution

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();
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top