0

I have around 50 EditText fields and I want to get their values using a loop. Getting the values individually as below works fine:

    SharedPreferences settings = getSharedPreferences(filename, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.clear(); //not sure if this is required

    final EditText t1 = (EditText) findViewById(R.id.text1Value);
    String T1 = t1.getText().toString();
    editor.putstring("text1",T1);

    final EditText t2 = (EditText) findViewById(R.id.text2Value);
    String T2 = t2.getText().toString();
    editor.putstring("text2", T2);

   ................................ and so on till EditText t50.

I have tried achieving this through the loop below but couldn't get it to work.

       for(int x=1; x<50; x++) 
    {
        EditText et[x] = (EditText) findViewById(R.id.text[x]Value);
        String t[x] = et[x].getText().toString();
        String Ref = "text" + x;
        editor.putString(Ref, t[x]);
    }
2
  • Yeah thanks mate. Still not exactly at a working state yet though. Commented Aug 2, 2016 at 13:32
  • dont forget to commit or apply the changes Commented Aug 2, 2016 at 13:44

3 Answers 3

2

You can try this by creating an array of the ids of the textview and looping through the array. Try this:

int[] ids = new int[]{R.id.text1Value,R.id.text2Value,R.id.text3Value};//and so on

int i =1;
for(int id : ids){
    EditText t = (EditText) findViewById(id);
    String Ref = "text" + i;
    editor.putString(Ref, t.getText().toString());
  i++;
}

Sign up to request clarification or add additional context in comments.

Comments

0

If I assume you have all of these EditTexts inside a Layout called ll (should work for structures other than Layout too, such as a ViewGroup etc.):

ArrayList<String> values = new ArrayList<String>();
for (int i = 0; i < ll.getChildCount(); i++) {
    if (ll.getChildAt(i) instanceof EditText) {
        EditText et = (EditText)ll.getChildAt(i);
        values.add(et.getText().toString());
    }
}

My Android is a little rusty so apologies if there is something wrong with that, but should give you the general idea anyway

Comments

0

int[] ids = new int[]{R.id.text1Value,R.id.text2Value,R.id.text3Value};//and so on

int i =1;
for(int id : ids){
    EditText t = (EditText) findViewById(id);
    String Ref = "text" + i;
    editor.putString(Ref, t.getText().toString());
  i++;
}

private void clearScreen() {
    int[] _ids = null;

    _ids = new int[]{R.id.etIdTAtividade, R.id.etNomeTAtividade, R.id.etNmAtividade};

    for (int i = 0; i < (_ids.length); i++) {
        EditText t = (EditText) findViewById(_ids[i]);
        t.setText("");
    }
}

1 Comment

Welcome to StackOverflow! Please could you add a description to this answer to tell the op how your solution works?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.