1

작은 안드로이드 앱을 만들었습니다. 목록보기에서 Google지도 활동을 열고지도를 길게 누르면 해당 위치의 주소가 ArrayList에 저장되고 목록보기, 장소의 latitute 및 longitute도 그런 식으로 저장됩니다, 그리고 나는 그 정보를 모두 SharedPreferences를 통해 저장합니다. 내 문제는 앱이 실행 중일 때 listview가 업데이트 되더라도 기본적으로 정보가 저장되지 않는다는 것을 의미하는 앱을 다시 시작할 때 모든 데이터가 손실된다는 것입니다. 데이터 저장은 MapsActivity의 onMapLongClick 메소드에서 수행되며 데이터로드는 MainActivity의 OnCreate 메소드에서 수행됩니다.android : SharedPreferences가 데이터를 저장하지 않습니다

MainActivity :

package com.rapha.favoriteplaces; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import java.io.IOException; 
import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 
    static ArrayList<String> places = new ArrayList<>(); 
    static ArrayList<String> lats = new ArrayList<>(); 
    static ArrayList<String> lngs = new ArrayList<>(); 
    static ArrayAdapter arrayAdapter; 

    @Override 
    public void onBackPressed() 
    { 

    } 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ListView listView = (ListView)findViewById(R.id.list); 

     places.add("Add a location..."); 

     final Intent intent = new Intent(getApplicationContext(), MapsActivity.class); 

     SharedPreferences sharedPreferences = this.getSharedPreferences("package com.rapha.favoriteplaces", Context.MODE_PRIVATE); 

     try { 
      places = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("places", ObjectSerializer.serialize(new ArrayList<String>()))); 

      lats = (ArrayList<String>)ObjectSerializer.deserialize(sharedPreferences.getString("lats", ObjectSerializer.serialize(new ArrayList<String>()))); 
      lngs = (ArrayList<String>)ObjectSerializer.deserialize(sharedPreferences.getString("lngs", ObjectSerializer.serialize(new ArrayList<String>()))); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     if(places.size() > 0 && lats.size() > 0 && lngs.size() > 0) 
     { 
      System.out.println("load data sucessfully check"); 
     } 

     arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, places); 
     listView.setAdapter(arrayAdapter); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       if(position == 0) 
       { 
        intent.putExtra("coisa", position); 
        startActivity(intent); 
       } 
       else 
       { 
        intent.putExtra("coisa", position); 
        String lat = ((lats.get(position - 1))); 
        String lon = ((lngs.get(position - 1))); 
        intent.putExtra("lat", lat); 
        intent.putExtra("lon", lon); 
        intent.putExtra("ad", places.get(position)); 

        startActivity(intent); 
       } 
      } 
     }); 
    } 
} 

MapsActivity :

package com.rapha.favoriteplaces; 

import android.*; 
import android.Manifest; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.pm.PackageManager; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.Build; 
import android.os.Handler; 
import android.support.annotation.NonNull; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.support.v4.content.ContextCompat; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.widget.Toast; 
import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener { 

    private GoogleMap mMap; 
    LocationManager locationManager; 
    LocationListener locationListener; 
    String address; 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) { 
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 100000, locationListener); 
      } 
     } 
    } 

    @Override 
    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); 
    } 
    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     Intent intent = getIntent(); 

     mMap.setOnMapLongClickListener(this); 
     int coisa = intent.getIntExtra("coisa", 0); 

     if(coisa == 0) 
     { 
      locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

      locationListener = new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 

        LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude()); 

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15)); 
       } 

       @Override 
       public void onStatusChanged(String provider, int status, Bundle extras) { 

       } 

       @Override 
       public void onProviderEnabled(String provider) { 

       } 

       @Override 
       public void onProviderDisabled(String provider) { 

       } 
      }; 
      if (Build.VERSION.SDK_INT < 23) { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100, locationListener); 
      } else { 
       if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
       } else { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100, locationListener); 
        mMap.clear(); 
        Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
        LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()); 
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15)); 
       } 
      } 
     } 
     else 
     { 
      double lat = Double.parseDouble(intent.getStringExtra("lat")); 
      double lon = Double.parseDouble(intent.getStringExtra("lon")); 
      LatLng location = new LatLng(lat, lon); 

      mMap.addMarker(new MarkerOptions().position(location).title(intent.getStringExtra("ad"))); 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15)); 
     } 
    } 
    @Override 
    public void onMapLongClick(LatLng latLng) { 

     Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 
     try { 
      List<Address> addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); 

      if (addresses != null && addresses.size() > 0) { 

       address = addresses.get(0).getAddressLine(0); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     mMap.addMarker(new MarkerOptions().position(latLng).title(address)); 
     MainActivity.lats.add(String.valueOf(latLng.latitude)); 
     MainActivity.lngs.add(String.valueOf(latLng.longitude)); 
     MainActivity.places.add(address); 
     MainActivity.arrayAdapter.notifyDataSetChanged(); 

     SharedPreferences sharedPreferences = getSharedPreferences("com.rapha.favoriteplaces",Context.MODE_PRIVATE); 

     try { 

      sharedPreferences.edit().putString("lats", ObjectSerializer.serialize(MainActivity.lats)).apply(); 
      sharedPreferences.edit().putString("lngs", ObjectSerializer.serialize(MainActivity.lngs)).apply(); 

      sharedPreferences.edit().putString("places", ObjectSerializer.serialize(MainActivity.places)).apply(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

오브젝트 시리얼 클래스 :

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 

public class ObjectSerializer { 

    public static String serialize(Serializable obj) throws IOException { 
     if (obj == null) return ""; 
     try { 
      ByteArrayOutputStream serialObj = new ByteArrayOutputStream(); 
      ObjectOutputStream objStream = new ObjectOutputStream(serialObj); 
      objStream.writeObject(obj); 
      objStream.close(); 
      return encodeBytes(serialObj.toByteArray()); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public static Object deserialize(String str) throws IOException { 
     if (str == null || str.length() == 0) return null; 
     try { 
      ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str)); 
      ObjectInputStream objStream = new ObjectInputStream(serialObj); 
      return objStream.readObject(); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public static String encodeBytes(byte[] bytes) { 
     StringBuffer strBuf = new StringBuffer(); 

     for (int i = 0; i < bytes.length; i++) { 
      strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a'))); 
      strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a'))); 
     } 

     return strBuf.toString(); 
    } 

    public static byte[] decodeBytes(String str) { 
     byte[] bytes = new byte[str.length()/2]; 
     for (int i = 0; i < str.length(); i += 2) { 
      char c = str.charAt(i); 
      bytes[i/2] = (byte) ((c - 'a') << 4); 
      c = str.charAt(i + 1); 
      bytes[i/2] += (c - 'a'); 
     } 
     return bytes; 
    } 
} 

답변

0

당신은에 넣고 두 개의 서로 다른 환경에서 점점 것으로 보인다.

환경 설정에서 가져 오는

, 당신은 package com.rapha.favoriteplaces을 사용하고 있습니다 :
SharedPreferences sharedPreferences = this.getSharedPreferences("package com.rapha.favoriteplaces", Context.MODE_PRIVATE); 
try { 
    places = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("places", ObjectSerializer.serialize(new ArrayList<String>()))); 
    // ... 

값을 저장

당신은 com.rapha.favoriteplaces을 사용

SharedPreferences sharedPreferences = getSharedPreferences("com.rapha.favoriteplaces",Context.MODE_PRIVATE); 
sharedPreferences.edit().putString("lats", ObjectSerializer.serialize(MainActivity.lats)).apply(); 

는 둘 다 동일한 확인하고 시도하십시오.

+0

나는 어리석은 실수를 저질렀다는 것을 믿을 수 없다. 너무 많이 고마워. 고맙다. 완벽하게 작동했다. – Motoboy

관련 문제