Android:在应用程序被杀/重新启动时将数据存储在应用程序中

时间:2015-10-18 12:07:57

标签: android caching android-activity

Android应用中的数据存储:我是Android和Android的新用户我有一个设置活动,其中我有国家和州,这是使用该应用程序的强制选项。当用户选择国家和州时,他可以根据自己的选择做某些操作。     这里的问题是,当他杀死应用程序时,数据会被删除,用户必须一次又一次地选择国家/地区。

Below is the code for Settings.java where there is a number picker for both country and state 

我已经选择了数字选择器来选择数据,当用户选择时,他必须在每次重新启动应用程序时选择。一旦应用程序被杀死,所有设置都将被重置。

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) {

    }
}

//这就是我从Picker中选择项目的方式我需要在本地存储它们以便在重新启动时无需重新选择。

2 个答案:

答案 0 :(得分:0)

您可以使用sharedpreferences类。

http://developer.android.com/training/basics/data-storage/shared-preferences.html

我希望这能解决你的问题。

答案 1 :(得分:0)

在共享prefrences here上查看本教程。共享Prefrences用于存储少量用户数据,如设置信息。您还可以使用prefrence片段查看教程here