Variable problem sharedpreferences

1

I'm doing an app where I try to create an event, save the date and time in a certain format and then retrieve them. Android Studio tells me

  

"can not find symbol"

as it does not find the variables. I define the variables later, but because of the structure of the program, I need you to read them not only later on, but also before, within public void . Are the variables show and show hours that you can not read in the first public void.

there goes the code:

@Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.setPadding(10, 10, 10, 10);
        map.moveCamera(CameraUpdateFactory.zoomTo(16.0f));
        map.setMyLocationEnabled(true);
        if( hasLocation(this) ) {
            float latitude = getLastLatitude(this);
            float longitude = getLastLongitude(this);
            float accuracy = getLastAccuracy(this);
            String street = getLastStreet(this);
            locationTextView.setText("Aparcaste en: ("+latitude+","+longitude+")");
            locationTextView.setVisibility(View.VISIBLE);
            for (BluetoothDevice device : btAdapter.getBondedDevices()) {
                if (device.getAddress().equals(carBtAddress)) {
                    {
                                            }
                }
            }
            if( street==null || street.length()==0 ) {
                if(Geocoder.isPresent()) {
                    Location last = new Location("be.ndusart");
                    last.setLatitude(latitude);
                    last.setLongitude(longitude);
                    (new GetAddressTask()).execute(last);

                }
            } else
                locationTextView.setText("Aparcaste en: " + street +" "+fechamostrar+ " " +horamostrar);
.
.
.
.


@Override
        protected void onPostExecute(String result){
            Date d=new Date();
            SimpleDateFormat fecc=new SimpleDateFormat("dd-MM-yyyy");
            String fechacComplString = fecc.format(d);
            SimpleDateFormat ho=new SimpleDateFormat("hh:mm:ss");
            String horaString = ho.format(d);
            SharedPreferences preferences = getSharedPreferences("MisPreferencias", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("hora", horaString);
            editor.putString("fecha", fechacComplString);
            editor.commit();
            String fechamostrar = preferences.getString("fecha","01-01-2017");
            String horamostrar = preferences.getString("hora","01-01-2017");
            if( result != null && result.length() > 0 ) {
                locationTextView.setText("Aparcaste en: "+result+ " "+fechamostrar+ " " +horamostrar);
            }
    
asked by José Ignacio Gavara 11.07.2017 в 18:34
source

1 answer

0

You must define the variables fechamostrar and horamostrar, as class variables, not within the method:

private String fechamostrar;
private String horamostrar;

and then assign the value within onPostExecute()

@Override
        protected void onPostExecute(String result){
        ...
        ...
        fechamostrar = preferences.getString("fecha","01-01-2017");
        horamostrar = preferences.getString("hora","01-01-2017");
         ...
         ...

        }

This way you would not have the problem that you mention in the line of code:

locationTextView.setText("Aparcaste en: " + street +" "+fechamostrar+ " " +horamostrar); 
    
answered by 12.07.2017 / 00:53
source