I am developing an app that requires a preferences page (using a checkbox) and after making it to display the selected interests when the user presses done, I would like to now save the checkbox information/the checked boxes. The goal is to have the user leave the checkbox page and then return to have the same boxes checked and the same result string.
With the current code below, I am able to check boxes, but when I hit 'Done' the page crashes. Did I not do the saving of the instance correct?
Maybe helpful Information:
1)I use the 'Done' button as a save button, and the 'loading' occurs when the page is created (if it is the first time the page is loaded, the list and string results are set to empty values.
2)The areas of the code that involve saving the data (where the crash is occuring) is "public void finalSelection" (the done button) and/or "public void onCreate" (on creation loading)
Thanks,
Larry
==================The Code=======================
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);
}
}
Aucun commentaire:
Enregistrer un commentaire