-2
ArrayList<String> checkedValue2;

i have a arrayList in which i add the checked value which working fine.checked/unchecked working fine but issue is when app restart the arrayList is empty.how to save the arraylist object .again when app restart and i check uncheck its value add remove. here is my code.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkedValue = new ArrayList<String>();

when i click on checkbox its value load to arraylist and when i uncheck the selected value remove when app restart the arraylist is empty plz tell me how to save arraylist aftar app restart.help me with some code

@Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        // TODO Auto-generated method stub
        cb = (CheckBox) v.findViewById(R.id.checkBox1);
        TextView tv = (TextView) v.findViewById(R.id.textView);
        pi = (ApplicationInfo) arg0.getItemAtPosition(position);
        cb.performClick();

        if (cb.isChecked()) {

            checkedValue.add(tv.getText().toString());
            itemCheckedd[position] = true;
           // Toast.makeText(MainActivity.this, "all" + position, Toast.LENGTH_LONG).show();
        } else if (!cb.isChecked()) {
            checkedValue.remove(tv.getText().toString());

        }

      }
xtylish
  • 115
  • 7

1 Answers1

0

hi please read this link 1>store data in saveInstanceState

    static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

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

2< retrieve data

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first

// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
    // Restore value of members from saved state
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
    // Probably initialize members with default values for a new instance
}
...

}

https://developer.android.com/training/basics/activity-lifecycle/recreating.html

you will store data in savedInstanceState and after activity restart get data from savedInstanceState

Vijay Rajput
  • 1,091
  • 1
  • 13
  • 18