如何在android中存储变量

时间:2014-10-20 11:08:27

标签: android variables android-spinner

我想在我的项目中存储一个变量。实际上,我希望用户通过下拉列表选择一个选项,并存储该变量的值,以便能够在我的其他活动中使用它

我的微调代码:

public class MyAndroidAppActivity extends Activity {

  private Spinner spinner1, spinner2;
  private Button btnSubmit;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addItemsOnSpinner2();
    addListenerOnButton();
    addListenerOnSpinnerItemSelection();
  }

  // add items into spinner dynamically
  public void addItemsOnSpinner2() {

    spinner2 = (Spinner) findViewById(R.id.spinner2);
    List<String> list = new ArrayList<String>();
    list.add("list 1");
    list.add("list 2");
    list.add("list 3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter);
  }

  public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
  }

  // get the selected dropdown list value
  public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    btnSubmit.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        Toast.makeText(MyAndroidAppActivity.this,
        "OnClickListener : " + 
                "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + 
                "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
            Toast.LENGTH_SHORT).show();
      }

    });
  }
}

2 个答案:

答案 0 :(得分:2)

您可以使用SharedPreferences存储信息,以便将来在您的应用中使用。

http://developer.android.com/reference/android/content/SharedPreferences.html http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

当然还有其他几种方法可以解决它,但这个方法很简单。

有关存储的更多信息,请访问:http://developer.android.com/guide/topics/data/data-storage.html#pref

答案 1 :(得分:0)

您可以通过两种方式实现这一目标:

第一种方法允许您仅在应用程序运行时保留数据,另一种方法即使应用程序关闭也会保留数据。

  1. 创建一个GlobalData类

    public enum GlobalData {
    
        Data;
    
        private boolean isBooleanVal= false;
    
    
        public boolean isBooleanVal() {
         return isBooleanVal;
        }
    
         public void setBooleanVal(boolean isQuizStartedAgain) {
           this.isQuizStartedAgain = isBooleanVal;
        }
    }
    
  2. 并在其他课程中调用它:

       GlobalData.Data.setBooleanVal(true);
    

    OR

       GlobalData.Data.isBooleanVal();
    
    1. 使用SharedPreferences

       private SharedPreferences sharedpreferences;
       private SharedPreferences.Editor editor;
      
       sharedpreferences = getActivity().getSharedPreferences(ApplicationConstants.SHARED_PREFERENCES, Context.MODE_PRIVATE);
       editor = sharedpreferences.edit();
       editor.putBoolean(value,value);
       editor.commit();
      
    2. Example