0

My app contains 25 edittexts. I am getting this 25 edittexts with the help of adapter class by giving count=25 and fitting in gridView by gridView.setAdapter(new TextAdapter(this)); in the activity class. So, the edittexts are dynamically generated. But the thing is I am unable to set the initial values in the edittexts. This is because the edittext objects are unavailable to set the values.

Suppose if I don't set any initial values in the edittexts and continue with my app. The same problem repeats while setting the values back in the edittexts which are entered in previous mode after changing the orientation. Because change in orientation creates new activity. Even I tried android:configChanges="orientation|keyboardHidden", but no use while I am setting the values back in the **onConfigurationChanged()**. Because I am setting the setContentView(); in the onConfigurationChanged() as I need the respective view, but still the edittext objects are unavailable to set their values.

Is there any solution to set back the values? If not, I am thinking(Might be completely wrong way, but as a newbie please go easy) to move the onCreate() method content to Application class. So the initial part goes to Application class including the creation of edittexts. and getting that edittext objects in the onCreate() method to set the values. Is it possible? Please suggest. Code snippet would be appreciated.

Spike
  • 147
  • 3
  • 17

1 Answers1

1

You will need to modify TextAdapter. Store the initial values in a String array, with the position of the String array element aligned to the position of the EditText in your GridView.

Pseudo-code (untested):

public class TextAdapter extends BaseAdapter {

String [] initial_value = {"Initial Value 1", "Initial Value 2", "Initial Value 3",  ..., };

    public View getView(int pos, View view, ViewGroup viewGroup) {

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater)    this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.edit_text_container, null);
        }

        (EditText) edtTemp = (EditText) view.findViewById(R.id.edit_text_id);
        edtTemp.setText(initial_value[pos]);
    }

}
Hang Guan
  • 102
  • 5
  • Hi Hang, Thanks a lot for your response. But Here I can't get the edittexts' references with id's as they are not defined in xml. So, I kept edittext.setText(initial_value[k]); in the if(view==null) block, but it still can't able to set. Please suggest. – Spike Sep 13 '12 at 14:14
  • Could you show me TextAdapter? The part where it inflates the layout / dynamically create it. – Hang Guan Sep 14 '12 at 00:10