how to save state of dynamically created editTexts

Posted by user922531 on Stack Overflow See other posts from Stack Overflow or by user922531
Published on 2012-11-25T22:58:39Z Indexed on 2012/11/25 23:03 UTC
Read the original article Hit count: 189

I'm stuck at how to save the state of my EditTexts on screen orientation. Currently if text is inputted into the EditTexts and the screen is orientated, the fields are wiped (as expected).

I am already calling onSaveInstanceState and saving a String, but I have no clue on how to save the EditTexts which are created in code and then retrieve them and add them to the EditTexts when redrawing the activity.

Snippet of my code:

My main activity is as follows:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // get the multidim array
    b = getIntent().getBundleExtra("obj");
    m = (Methods) b.getSerializable("Methods"); 

            // method to draw the layout
    InitialiseUI();

    // Restore UI state from the savedInstanceState.
    if (savedInstanceState != null) {
        String strValue = savedInstanceState.getString("light");
        if (strValue != null) {
            FLight = strValue;
        }
    }
    try {
        mCamera = Camera.open();
        if (FLight.equals("true")) {
            flashLight();
        }
    } catch (Exception e) {
        Log.d(TAG, "Thrown exception onCreate() camera: " + e);
    }

} // end onCreate

/** Called when the back button is pressed. */
@Override
public void onResume() {
    super.onResume();
    try {
        mCamera = Camera.open();
        if (FLight.equals("true")) {
            flashLight();
        }
    } catch (Exception e) {
        Log.d(TAG, "Thrown exception onCreate() camera: " + e);
    }
} // end onCreate

/** saves data before leaving the screen */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("light", FLight);
}

/** called when exiting / leaving the screen */
@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG, "onPause()");
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
}



/*
 * set up the UI elements - add click listeners to buttons used in
 * onCreate() and onConfigurationChanged() 
 * 
 * Set the editTexts fields to show the previous readings as Hints
 */
public void InitialiseUI() {
    Log.d(TAG, "Start of InitialiseUI, Main activity"); 

    // get a reference to the TableLayout
    final TableLayout myTLreads = (TableLayout) findViewById(R.id.myTLreads);

    // Create arrays to hold the TVs and ETs
    final TextView[] myTextViews = new TextView[m.getNoRows()]; // create an empty array;
    final EditText[] myEditTexts = new EditText[m.getNoRows()]; // create an empty array;

    for(int i =0; i<=m.getNoRows()-1;i++ ){
        TableRow tr=new TableRow(this);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        // create a new textview / editText
        final TextView rowTextView = new TextView(this);
        final EditText rowEditText = new EditText(this);

        // setWidth is needed otherwise my landscape layout is OFF
        rowEditText.setWidth(400);

        // this stops the keyboard taking up the whole screen in landscape layout
        rowEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);

        // add some padding to the right of the TV
        rowTextView.setPadding(0,0,10,0);

        // set colors to white
        rowTextView.setTextColor(Color.parseColor("#FFFFFF"));
        rowEditText.setTextColor(Color.parseColor("#FFFFFF"));

        // if readings already sent today set color to yellow
        if(m.getTransmit(i+1)==false){
            rowEditText.setEnabled(false);
            rowEditText.setHintTextColor(Color.parseColor("#FFFF00"));
        }

        // set the text of the TV to the meter name
        rowTextView.setText(m.getMeterName(i+1));
        // set the hint of the ET to the last submitted reading
        rowEditText.setHint(m.getLastReadString(i+1));

        // add the textview to the linearlayout
        rowEditText.setInputType(InputType.TYPE_CLASS_PHONE);//InputType.TYPE_NUMBER_FLAG_DECIMAL);
        tr.addView(rowTextView);
        tr.addView(rowEditText);
        myTLreads.addView(tr);

        // add a reference to the textView
        myTextViews[i] = rowTextView;
        myEditTexts[i] = rowEditText;

    }


    final Button submit = (Button) findViewById(R.id.submitReadings);
    // add a click listener to the button

    try {
        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d(TAG, "Submit button clicked, Main activity");

                preSubmitCheck(m.getAccNo(), m.getPostCode(), myEditTexts); // method to do HTML getting and sending

            }
        });
    } catch (Exception e) {
        Log.d(TAG, "Exceptions (submit button)" + e.toString());
    }

}// end of InitialiseUI

I don't need to do anything with these values until a button is clicked. Would it be easier if they were a ListView, i'm guessing I would still have the problem of saving them and retrieving them on rotation. If it helps I have an object m which is a string[][] I could temporarily somehow store them in

© Stack Overflow or respective owner

Related posts about android

Related posts about edittext