保存复选框信息 Android 应用开发

Saving Checkbox Information Android App Development

我正在开发一个需要首选项页面(使用复选框)的应用程序,并在用户按下完成时显示所选兴趣后,我现在想保存复选框 information/the checked boxes .目标是让用户离开复选框页面,然后 return 选中相同的框和相同的结果字符串。

使用下面的当前代码,我可以选中复选框,但是当我点击 'Done' 时,页面崩溃了。我没有正确保存实例吗?

可能有用的信息:

1)我使用'Done'按钮作为保存按钮,'loading'出现在页面创建时(如果是第一次加载页面,列表和字符串结果设置为空值。

2) 涉及保存数据的代码区域(发生崩溃的地方)是 "public void finalSelection"(完成按钮)and/or "public void onCreate"(创建加载时)

谢谢,

拉里

==================代码=======================

public class Interests extends Activity {

  static final String STATE_Selection="arrayInterests";
  static final String STATE_Interests="stringInterests";
  public ArrayList<String> selection;
  public String final_interests;
  TextView final_text;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      if (savedInstanceState != null) {
          // Restore value of members from saved state
          selection = savedInstanceState.getStringArrayList(STATE_Selection);
          final_interests = savedInstanceState.getString(STATE_Interests);
      } else {
          selection  = new ArrayList<String>();
          final_interests= "";
      }
      setContentView(R.layout.activity_interests);
      final_text = (TextView)findViewById(R.id.final_result);
      final_text.setEnabled(false);
  }

  @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_interests, 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);
}


public void selectItem(View view){
    boolean checked = ((CheckBox) view).isChecked();

        switch (view.getId())
        {
            case R.id.baby_box:
                if(checked)
                {selection.add("Baby");}

                else
                {selection.remove("Baby");}
                break;
            case R.id.travel_box:
                if(checked)
                {selection.add("Travel");}
                else
                {selection.remove("Travel");}
                break;
            case R.id.electronics_box:
                if(checked)
                {selection.add("Electronics");}
                else
                {selection.remove("Electronics");}
                break;
            case R.id.services_box:
                if(checked)
                {selection.add("Services");}
                else
                {selection.remove("Services");}
                break;
            case R.id.entertainment_box:
                if(checked)
                {selection.add("Entertainment");}
                else
                {selection.remove("Entertainment");}
                break;
            case R.id.sports_box:
                if(checked)
                {selection.add("Sports");}
                else
                {selection.remove("Sports");}
                break;
            case R.id.garden_box:
                if(checked)
                {selection.add("Garden");}
                else
                {selection.remove("Garden");}
                break;
            case R.id.outdoors_box:
                if(checked)
                {selection.add("Outdoors");}
                else
                {selection.remove("Outdoors");}
                break;
            case R.id.books_box:
                if(checked)
                {selection.add("Books");}
                else
                {selection.remove("Books");}
                break;
            case R.id.auto_box:
                if(checked)
                {selection.add("Auto");}
                else
                {selection.remove("Auto");}
                break;
            case R.id.food_box:
                if(checked)
                {selection.add("Food");}
                else
                {selection.remove("Food");}
                break;
            case R.id.familyfriends_box:
                if(checked)
                {selection.add("Family and Friends");}
                else
                {selection.remove("Family and Friends");}
                break;
            case R.id.music_box:
                if(checked)
                {selection.add("Music");}
                else
                {selection.remove("Music");}
                break;
            case R.id.office_box:
                if(checked)
                {selection.add("Office");}
                else
                {selection.remove("Office");}
                break;
            case R.id.grocery_box:
                if(checked)
                {selection.add("Grocery");}
                else
                {selection.remove("Grocery");}
                break;
            case R.id.pets_box:
                if(checked)
                {selection.add("Pets");}
                else
                {selection.remove("Pets");}
                break;
            case R.id.homeimprovement_box:
                if(checked)
                {selection.add("Home Improvement");}
                else
                {selection.remove("Home Improvement");}
                break;
            case R.id.jewelery_box:
                if(checked)
                {selection.add("Jewelery");}
                else
                {selection.remove("Jewelery");}
                break;
            case R.id.decor_box:
                if(checked)
                {selection.add("Decor");}
                else
                {selection.remove("Decor");}
                break;
            case R.id.beauty_box:
                if(checked)
                {selection.add("Beauty");}
                else
                {selection.remove("Beauty");}
                break;
            case R.id.toysgames_box:
                if(checked)
                {selection.add("Toys and Games");}
                else
                {selection.remove("Toys and Games");}
                break;
            case R.id.fashion_box:
                if(checked)
                {selection.add("Fashion");}
                else
                {selection.remove("Fashion");}
                break;
            case R.id.health_box:
                if(checked)
                {selection.add("Health");}
                else
                {selection.remove("Health");}
                break;
            case R.id.artscrafts_box:
                if(checked)
                {selection.add("Arts and Crafts");}
                else
                {selection.remove("Arts and Crafts");}
                break;
        }

}

public void finalSelection(View view, Bundle savedInstanceState){
    for(String Selections : selection)
    {
        final_interests= final_interests + Selections + "\n";
    }
    final_text.setText(final_interests);
    final_text.setEnabled(true);
    // Save the user's current game state
    savedInstanceState.putStringArrayList(STATE_Selection, selection);
    savedInstanceState.putString(STATE_Interests, final_interests);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

}

为了回答您的问题并阐述其他评论,对于应用程序使用的数据存储,而不是状态更改信息,如果您需要大量信息,请使用 SharedPreferences 或 Sqlite 数据库。