Getting off shared preferencies
This commit is contained in:
parent
eb61b3fdb7
commit
5c58efae68
@ -4,6 +4,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -16,10 +17,6 @@ public class AddCity extends AppCompatActivity {
|
|||||||
|
|
||||||
private EditText cityName;
|
private EditText cityName;
|
||||||
|
|
||||||
// - Define shared preferences var -
|
|
||||||
SharedPreferences sharedPreferences;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
@ -39,26 +36,15 @@ public class AddCity extends AppCompatActivity {
|
|||||||
{
|
{
|
||||||
cityName = (EditText) findViewById(R.id.editTextNewCity);
|
cityName = (EditText) findViewById(R.id.editTextNewCity);
|
||||||
final String newCityString = cityName.getEditableText().toString();
|
final String newCityString = cityName.getEditableText().toString();
|
||||||
if(newCityString.length() == 0)
|
if(newCityString.isEmpty())
|
||||||
{
|
{
|
||||||
cityName.setError("Please, enter a city name");
|
cityName.setError("Please, enter a city name");
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
|
|
||||||
// - Load Shared Preferences (named 'prefs') -
|
|
||||||
sharedPreferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
|
|
||||||
|
|
||||||
// - Now the ImageView has to be hidden, so write it into the shared preferences -
|
|
||||||
// - Put value '0' into the preference 'umbrella_visible' of the shared preference book called 'prefs'
|
|
||||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
|
||||||
editor.putInt("umbrella_visible", 0);
|
|
||||||
editor.apply();
|
|
||||||
|
|
||||||
saveCity(newCityString);
|
saveCity(newCityString);
|
||||||
resetForm();
|
resetForm();
|
||||||
|
openAddCityPage();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -73,4 +59,10 @@ public class AddCity extends AppCompatActivity {
|
|||||||
CityRepository.getInstance(this).addCity(new City(cityName));
|
CityRepository.getInstance(this).addCity(new City(cityName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void openAddCityPage()
|
||||||
|
{
|
||||||
|
Intent intent = new Intent(AddCity.this, MainActivity.class);
|
||||||
|
startActivity(intent);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2,31 +2,59 @@ package fr.romanet.vj.apps.myweather;
|
|||||||
|
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.EditText;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
import android.widget.ListView;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
|
|
||||||
|
import org.w3c.dom.Text;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import fr.romanet.vj.apps.myweather.bo.City;
|
||||||
|
import fr.romanet.vj.apps.myweather.repository.CityRepository;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
// - Umbrella ImageView
|
private ListView listViewCityNames;
|
||||||
public static ImageView imgPara;
|
private FloatingActionButton buttonPlus;
|
||||||
|
private ImageView imgPara;
|
||||||
// - Is used to store the shared preference value of umbrella visibility
|
private TextView addCityText;
|
||||||
public int umbrella_visible;
|
|
||||||
|
|
||||||
// - Define shared preferences var -
|
|
||||||
SharedPreferences sharedPreferences;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
FloatingActionButton buttonPlus = (FloatingActionButton) findViewById(R.id.plusButton);
|
|
||||||
|
listViewCityNames = findViewById(R.id.listeCity);
|
||||||
|
buttonPlus = (FloatingActionButton) findViewById(R.id.plusButton);
|
||||||
|
imgPara = (ImageView) findViewById(R.id.imageView);
|
||||||
|
addCityText = (TextView) findViewById(R.id.textViewAddCity);
|
||||||
|
|
||||||
|
initialisingComponents();
|
||||||
|
|
||||||
|
listViewCityNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
||||||
|
String currentCityName = adapterView.getItemAtPosition(i).toString();
|
||||||
|
Intent intent = new Intent(MainActivity.this, ShowTemparature.class);
|
||||||
|
intent.putExtra( "cityName", currentCityName);
|
||||||
|
startActivity(intent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
buttonPlus.setOnClickListener(new View.OnClickListener() {
|
buttonPlus.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view)
|
public void onClick(View view)
|
||||||
@ -34,46 +62,39 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
openAddCityPage();
|
openAddCityPage();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// - Assign Activity ImageView to var -
|
|
||||||
imgPara = (ImageView) findViewById(R.id.imageView);
|
|
||||||
|
|
||||||
// - Load Shared Preferences (named 'prefs') -
|
|
||||||
sharedPreferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
|
|
||||||
|
|
||||||
// - Try to retrieve 'umbrella_visible', if not set return 3 -
|
|
||||||
umbrella_visible = sharedPreferences.getInt("umbrella_visible", 3);
|
|
||||||
|
|
||||||
// - If preference not yet defined, set value to 1 (visible) -
|
|
||||||
if (umbrella_visible == 3){
|
|
||||||
|
|
||||||
// - Put value '1' into the preference 'umbrella_visible' of the shared preference book called 'prefs'
|
|
||||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
|
||||||
editor.putInt("umbrella_visible", 1);
|
|
||||||
editor.apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d("DEBUG1 -> create", Integer.toString(umbrella_visible));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume()
|
||||||
|
{
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
//initList();
|
||||||
|
}
|
||||||
|
|
||||||
// - Load Shared Preferences (named 'prefs') -
|
public void initialisingComponents()
|
||||||
sharedPreferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
|
{
|
||||||
|
final List<City> city = CityRepository.getInstance(this).getCity();
|
||||||
// - Try to retrieve 'umbrella_visible', if not set return 3 -
|
if(city.isEmpty())
|
||||||
umbrella_visible = sharedPreferences.getInt("umbrella_visible", 3);
|
{
|
||||||
Log.d("DEBUG1 -> resume", Integer.toString(umbrella_visible));
|
listViewCityNames.setVisibility(View.GONE);
|
||||||
|
|
||||||
// - If 'umbrella_visible' is equal to '0', we have to hide the ImageView -
|
|
||||||
if(umbrella_visible == 0){
|
|
||||||
|
|
||||||
// - Hide ImageView -
|
|
||||||
imgPara.setVisibility(View.GONE);
|
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
imgPara.setVisibility(View.GONE);
|
||||||
|
addCityText.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
initList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initList()
|
||||||
|
{
|
||||||
|
final List<City> city = CityRepository.getInstance(this).getCity();
|
||||||
|
ArrayList<String> listeExample = new ArrayList<String>();
|
||||||
|
for(int i = 0; i < city.size(); i++)
|
||||||
|
{
|
||||||
|
listeExample.add("" + city.get(i).nameCity);
|
||||||
|
}
|
||||||
|
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listeExample);
|
||||||
|
listViewCityNames.setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void openAddCityPage()
|
public void openAddCityPage()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user