保存Checkbox信息Android App开发

时间:2015-07-02 15:44:58

标签: android checkbox save instance state

我正在开发一个需要首选项页面的应用程序(使用复选框),并在用户按下完成后显示所选兴趣之后,我想保存复选框信息/复选框。目标是让用户离开复选框页面,然后返回以检查相同的框和相同的结果字符串。

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

也许有用的信息:

1)我使用“完成”按钮作为保存按钮,并在创建页面时发生“加载”(如果是第一次加载页面,则列表和字符串结果将设置为空值

2)涉及保存数据的代码区域(崩溃发生的地方)是“public void finalSelection”(完成按钮)和/或“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);
}

}

1 个答案:

答案 0 :(得分:1)

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