2

Hello everybody I have an edit text and button when I edited some value in edittext on clicking button done, the value need to store in a string array can any one help in fixing of this task ,Thanks in advance

3
  • 2
    please copy your code so, that we can help you Commented Mar 31, 2015 at 7:54
  • 2
    Why should be this question upvoted? Commented Mar 31, 2015 at 7:58
  • 1
    This will help you i feel android-mantra.blogspot.in/2013/09/… Commented Mar 31, 2015 at 7:58

3 Answers 3

3

Try Array List. use the following code in your main java

final ArrayList<String> list = new ArrayList<String>();

Button button= (Button) findViewById(R.id.buttonId);
final EditText editText = (EditText) findViewById(R.id.editTextId)
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        list.add(editText.getText().toString());
    }
});

// to get i th element
int i=0;
Log.d("value", list.get(i));
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

...
int[] arr = new int[];

//listener
onclick()
  {
    arr = arr.addElement(arr,Integer.parseInt(et.getText().toString()));
  }

 }//oncreate end

static int[] addElement(int[] a, int e) {
a  = Arrays.copyOf(a, a.length + 1);
a[a.length - 1] = e;
return a;
} 

2 Comments

you can change int array into string array as per requirement.
Just make sure to pass int values for int array and string for string array in addElement func from your listener's onclick :)
0

you should use List at place of string array, List is dynamic growing array so you can easily add and remove items from List as below:

List<String> list = new ArrayList<String>();
if(button clicked)
{
   list.add(editText.getText().toString());
}

or if you are bound to use array of string in any case then follow "Kay" solution as well in that case you need to increment in item index in array before adding item into array.

Comments