2015-10-18 82 views
-1

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

    } 
} 

//這是我如何選擇從選擇器項目,我需要將它們存儲在本地,這樣在重新開張沒有麻煩重新選擇。

回答

0

查看本教程共享的首選here。共享的特權用於存儲少量的用戶數據,如設置信息。您也可以使用prefrence片段檢查教程here

+0

感謝它幫助 – user1664899

相關問題