1

I was wondering how to make a code to let the user input values into a dynamic array, so lets say the first box the user enters 5, this will be the array length, then he enters another 5 numbers for the array, how do I make this dynamic, instead of assuming the array length will always be 5? and what do we use to enter the data, EditText? if so, does the EditText go inside a loop like :

  for(i=0;i<arrlength;i++)
  { 
    arr[i] = (EditText)findViewById(R.id.txtbound);
  }

would this be how I retrieve the data from the user? and after the user enters the specified values, in this case 5 values, how do we enter new screen? should I put a :

if(i == arrlength) startActivity(i);

inside the for loop?

Any help would be greatly appreciated

1 Answer 1

2

From your description, it looks like you want to create a number of different EditText boxes after you enter the initial value. Let's handle the dynamic array first. Have you looked at any of the implementations of the Android List interface? An ArrayList can be used as follows:

ArrayList<Integer> integerList = new ArrayList<Integer>();
integer.add(1);
integer.add(2);

To pass data into a new activity, please see this post.

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

4 Comments

Hey, yeah I know how to pass data to a new activity, and if I use your example above will it only allow for two values in the array?
@david No, the arraylist will grow as you add elements
Hey, sorry, I tried to use your code but it wont work, it says that there's misplaced construct(s), could you show me what xml layout you'd use ?
@david For what? The ArrayList code is pretty general and does not necessarily apply to a specific layout. Now, if you want to add a new EditText with each entry, you may need to dynamically add things to the Layout.

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.