Android应用程序中的数据存储:我是Android新手&我有一个设置活动,其中我有国家和州,这是使用该应用程序的必需选项。当用户选择国家和地区时,他可以根据自己的选择采取某些行动。 这里的问题是,当他杀死应用程序时,数据被删除,用户必须一次又一次地选择国家/州。Android:当应用程序被杀/重新启动时将数据存储在应用程序
Below is the code for Settings.java where there is a number picker for both country and state
在哪里我已数拾荒者当用户选择一次,他有他每次重新启动的app.Once应用程序被杀死了所有设置被重置的时间来选择,现在选择数据。
Settings.java
package com.app.info.app
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;
import com.app.info.app.Model.Country;
import com.app.info.app.Singleton.BigBoss;
public class Settings extends AppCompatActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks, NumberPicker.OnValueChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//Country Picker
final NumberPicker array1 =
(NumberPicker)findViewById(R.id.countrypicker);
array1.setMinValue(0);
array1.setMaxValue(BigBoss.getInstance().countries.length-1);
array1.setDisplayedValues(BigBoss.getInstance().getCountryNames());
array1.setWrapSelectorWheel(true);
array1.setOnValueChangedListener(this);
//Tip picker where user choose the desired Tipping
NumberPicker statepick = (NumberPicker)findViewById(R.id.statepicker);
statepick.setWrapSelectorWheel(true);
statepick.setOnValueChangedListener(this);
//Tip picker
NumberPicker tippick = (NumberPicker)findViewById(R.id.tippicker);
tippick.setDisplayedValues(new String[] { "5%", "10%", "15%","20%","25%" });
tippick.setMinValue(1);// restricted number to minimum value i.e 1
tippick.setMaxValue(5);// restricked number to maximum value i.e. 31
tippick.setWrapSelectorWheel(true);
tippick.setOnValueChangedListener(this);
}
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if (picker.getId() == R.id.countrypicker) {
Country selectedCountry = BigBoss.getInstance().countries[newVal];
BigBoss.getInstance().currentCountrySelected = selectedCountry;
} // Here I am using the choosed country for some calculations
}
public void countryClicked(View v) {
NumberPicker np = (NumberPicker)findViewById(R.id.countrypicker);
NumberPicker statepick = (NumberPicker)findViewById(R.id.statepicker);
NumberPicker tippick = (NumberPicker)findViewById(R.id.tippicker);
TextView countrytv = (TextView) findViewById(R.id.countryval);
TextView statetv = (TextView) findViewById(R.id.stateval);
TextView tiptv = (TextView) findViewById(R.id.tipval);
statepick.setVisibility((View.GONE));
tippick.setVisibility((View.GONE));
if(np.getVisibility()==View.VISIBLE){
np.setVisibility(View.GONE);
} else {
np.setVisibility(View.VISIBLE); // Hiding the picker when one is choosed
}
}
public void stateClicked(View v) {
NumberPicker np = (NumberPicker)findViewById(R.id.countrypicker);
NumberPicker statepick = (NumberPicker)findViewById(R.id.statepicker);
statepick.setMinValue(0);
String name ="India" ;
if (BigBoss.getInstance().currentCountrySelected != null) {
name = BigBoss.getInstance().currentCountrySelected.name;
}
statepick.setMaxValue(BigBoss.getInstance().getStateNamesForCountry(name).length-1);
statepick.setDisplayedValues(BigBoss.getInstance().getStateNamesForCountry(name)); // Get selected Country Above
NumberPicker tippick = (NumberPicker)findViewById(R.id.tippicker);
np.setVisibility((View.GONE));
tippick.setVisibility((View.GONE));
if(statepick.getVisibility()==View.VISIBLE){
statepick.setVisibility(View.GONE);
} else {
statepick.setVisibility(View.VISIBLE); // Setting the visibility of elements
}
}
public void tipClicked(View v) {
NumberPicker np = (NumberPicker)findViewById(R.id.countrypicker);
NumberPicker statepick =(NumberPicker)findViewById(R.id.statepicker);
NumberPicker tippick = (NumberPicker)findViewById(R.id.tippicker);
np.setVisibility(View.GONE);
statepick.setVisibility((View.GONE));
if(tippick.getVisibility()==View.VISIBLE){
tippick.setVisibility((View.GONE));
} else {
tippick.setVisibility(View.VISIBLE);
}
}
public void aboutUsclicked(View v)
{
Intent aboutus1 = new Intent(v.getContext(), AboutUs.class);
startActivity(aboutus1);
}
public void feedBackclick(View v)
{
Intent feed1 = new Intent(v.getContext(), FeedBack.class);
startActivity(feed1);
}
public void logout(View v) {
TextView logout = (TextView) findViewById(R.id.logout);
// Logout Button Click Listener
logout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Logout current userParseUser currentUser = ParseUser.getCurrentUser();
ParseUser.logOut();
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.setEmail("");
currentUser.setPassword("");
currentUser.setUsername("");
Intent homebk = new Intent(v.getContext(), MainActivity.class);
startActivity(homebk);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onNavigationDrawerItemSelected(int position) {
}
}
//这是我如何选择从选择器项目,我需要将它们存储在本地,这样在重新开张没有麻烦重新选择。
感谢它帮助 – user1664899