how to List double values from dynamically added EditTexts android - android

I want to create a list of the double values. screen shot of my layout
When the (+) add button is pressed a minimum of three edit boxes are added.
i want to get the values of these edit boxes and be able to cross multiply them.
the values will be mostly +/- and with decimals.
How can i identify the edit boxes, then from the input Values, i set them in a List where i can be able to cross Multiply them.
I am trying this but i don't understand where am going wrong.
List<EditText> allNs = new ArrayList<EditText>();
List<EditText> allEs = new ArrayList<EditText>();
String[] northings = new String[allNs.size()];
String[] eastings = new String[allEs.size()];
double inputNorths, inputEasts = 0;
for(int i=0; i<allNs.size(); i++){
northings[i] = allNs.get(i).getText().toString();
inputNorths = Double.parseDouble(northings[i]);
northValues [1] = inputNorths;
}
resultN.add(northValues);
for(int e=0; e<allEs.size(); e++){
eastings[e] = allEs.get(e).getText().toString();
inputEasts = Double.parseDouble(eastings[e]);
eastValues[2] = inputEasts;
}
resultsE.add(eastValues);
calcArea();

To be honest I don't understand what are you doing here:
inputNorths = Double.parseDouble(northings[i]);
....
inputEasts = Double.parseDouble(eastings[e]);
Every loop you overwrite these variables. Maybe you forgot about adding?
inputNorths += Double.parseDouble(northings[i]);
northValues [1] += inputNorths;

Related

How to iterate controls to get their value in a loop in Android

I have many controls in an activity layout in Android, and
i want to get their value in a simple for, the problem is that
Eclipse assigns the ID randomy, so I cannot get controls ID in a
sequential way to access the controls in a for loop because
they are not sequential IDs.
Here is why I am trying to do:
int visibleId = R.id.fieldVisible1;
int editId = R.id.editField2;
int spinnerId = R.id.spinner1;
for(int nIndex = 0; nIndex < 12; nIndex++)
{
CheckBox checkBox = (CheckBox)findViewById(visibleId + nIndex);
checkBox.setChecked(_fieldsInfoArray.get(nIndex).getVisible() == 1);
EditText edit = (EditText)findViewById(editId );
edit.setText(_fieldsInfoArray.get(nIndex).getName());
Spinner spinner = (Spinner)findViewById(spinnerId + nIndex);
spinner.setSelection(_fieldsInfoArray.get(nIndex).getOffset());
}
how can I make Eclipse to assign sequential IDs to controls so I can
just increment by 1 the id in a loop?
A better way of doing this may possibly to look into the use of:
int children = layout.getChildCount();
for (int i=0; i < children; i++){
View v = ll.getChildAt(i);
}
This would allow you to loop over all views inside the layout without needing to know their IDs.

creating dynamic resizable layout based on content

I am trying to create a layout, that shows current 3 hour lessons along with the time and date with room number.
possible screens:
The room and time/date is always static at the top and the rest would be dynamic from calls in SQL from JSON.
data = idleResponse.getJSONArray("lecture");
ArrayList<String> lects = new ArrayList<String>();
for (int i = 0; i < data.length(); i++) {
JSONObject jObj = data.getJSONObject(i);
String time = jObj.getString("startTime"); // to do.substring(0, 4);
String moduleName = jObj.getString("moduleName");
lects.add(time + " " + moduleName);
}
String[] lectureList = new String[lects.size()];
for (int i = 0; i<lects.size();i++){
// fill with data
lectureList[i] = lects.get(i);
}
lecture.setText("Upcomming Lectures:");
//set to list view
ListView listitems=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lectureList);
listitems.setAdapter(adapter);
I tried to implement this with a list view, however it just shrinks down to a small section of the screen based on number of elements.
as you can see it just shrinks down.
I was wondering what would be the best type of layout to use for this type of problem, all of my layouts are pretty basic and something like this seems quite a challenge for me thanks.
You would have to write your own Adapter for listview and use weight property for layout of each row

Android: Fill array up to a certain length

The situation
I have got different char[] arrays with a different amount of items. To avoid the "OutOfBounds"-Error while processing them I want to standardize them.
For Example:
My Array has following items: "5;9" --> What I want it to look like: "0,0,0,0,5,9" (fill up with "0"s to 6 items)
What I tried:
char[] myarray1 = mystring1.toCharArray();
...
for(int i=0; i<6; i++){
myarray1[i] = 0;
if(i<myarray1.length-1){
myarray1[i] = myarray1[i];
}else{
myarray1[i] = 0;
};
};
My code failed, because it evokes exactly that error...
I hope somebody can help me :)
Thanks!
The reason why your solution doesn't work is that you are using the same array for everything.
After char[] myarray1 = mystring1.toCharArray(); the length of myarray1 is 2, so you cannot simply assign entry 2,3,4 and 5 in your loop. Furthermore if you want the character ´0´ to be in the string, you need to surround your zeros with apostrophes.
You can fix your solution like this:
char[] myNewArray = new char[6];
int myarrayIndex = 0;
for(int i=0; i<6; i++)
{
if(i < (myNewArray.length - myarray1.length)) {
myNewArray[i] = '0';
} else {
myNewArray[i] = myarray1[myarrayIndex++];
};
};
System.out.println(myNewArray); //Will print out: 000059
An easier solution could be this:
myarray1 = String.format("%6s", mystring1).replace(' ', '0').toCharArray();
Firstly trying to fill 0's is not going to fix the error.
Secondly your logic is not right(assuming size as 6), change it to myString.length().
And I don't understand the point of myarray1[i] = myarray1[i];.
Anyways, every array with integer size is initialized by Zero's according to Java specs. On the other hand if you want to fill it with any other value, try Arrays.fill().
I think this function will accomplish what you're trying to do.
private static String formatString(String input)
{
String FORMATTED_STRING = "0,0,0,0,0,0";
int difference = FORMATTED_STRING.length() - input.length();
return FORMATTED_STRING.substring(0, difference) + input;
}

How to get EditText ID

I have added some EditTexts using a loop,
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
}
}
After this I want to get the text that the user adds into the EditText fields, but I am stuck on how to do this. How would I get an Id for each of these EditText Fields?
Thanks, Oli
just add them to a collection when you're creating them that you can easily refer to by index or key or loop through or otherwise.
EditText[] etCollection = new EditText[8];
..........
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
etCollection[i] = ed; <------ adding them to the collection
}
}
You'll have to do ed.setId( id ); on each one your create, and id should be unique for each one you want to find. When you go to find one do
EditText ed = (EditText)findViewById( id );
You'll have to keep track of your ids somewhere.
Alternatively you can just keep a list of the EditTexts that you create and run through the list to get the text of each one.

Array integer Android

ok so i create an array that has integers. The array displays five number from the min and max. How can i display all five numbers in a textview or edittext ? I tried:
nameofile.setText(al.get(x).toString());
but it only displays one?
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
String myString = TextUtils.join(", ", al);
lottonumbers.setText(myString);
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(0);
al.add(1);
al.add(5);
al.add(4);
al.add(3);
java.util.Collections.sort(al);//for sorting Integer values
String listString = "";
for (int s : al)
{
listString += s + " ";
}
nameofile.setText(listString);
You're currently only printing out one element (the one at index x). If you want to print them all in order, you can just join them using TextUtils.join().
Update: After seeing your edit, I think there's a better way to go about what you're trying to do. Instead of trying to pull the values one at a time, and update the list, why not just shuffle them, then use the above method?
Update 2: Okay, I think I finally understand your problem. Just a simple change, then.
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
StringBuilder text = new StringBuilder(); // Create a builder
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
if (i > 0)
text.append(", "); // Add a comma if we're not at the start
text.append(x);
}
lottonumbers.setText(text);
al.get(x).toString() will only get the value at index "x". If you want to display all values, you need to combine all of the values from the array into a single string then use setText on that string.
You are only showing one number of your array in the TextView, you must to concat the numbers to see the others results like:
for(Integer integer : al) {
nameofile.setText(nameofile.getText() + " " + al.get(x).toString());
}
Then i think you can show all number in one String.

Categories

Resources