well the calculation in my app goes like this..
have a edittext box where the input(number) from the user is parsed to int as nos.
based on the input two other sets of edittext box appears.
if nos is 3 the 3 edittext from arrayOfEditText and 3 edittext from arrayOfEditText1 appears..
and the value entered in those sets of edittext are used for calculation.
the incomplete java code is below..
calculate.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
// TODO Auto-generated method stub
chk();
}
});
}
public void chk() {
EditText[] arrayOfEditText = new EditText[11];
arrayOfEditText[1] = ((EditText)findViewById(R.id.EditText01));
arrayOfEditText[2] = ((EditText)findViewById(R.id.EditText02));
arrayOfEditText[3] = ((EditText)findViewById(R.id.EditText03));
arrayOfEditText[4] = ((EditText)findViewById(R.id.EditText04));
arrayOfEditText[5] = ((EditText)findViewById(R.id.EditText05));
arrayOfEditText[6] = ((EditText)findViewById(R.id.EditText06));
arrayOfEditText[7] = ((EditText)findViewById(R.id.EditText07));
arrayOfEditText[8] = ((EditText)findViewById(R.id.EditText08));
arrayOfEditText[9] = ((EditText)findViewById(R.id.EditText09));
arrayOfEditText[10] = ((EditText)findViewById(R.id.EditText10));
EditText[] arrayOfEditText1 = new EditText[11];
arrayOfEditText1[1] = ((EditText)findViewById(R.id.EditText11));
arrayOfEditText1[2] = ((EditText)findViewById(R.id.EditText12));
arrayOfEditText1[3] = ((EditText)findViewById(R.id.EditText13));
arrayOfEditText1[4] = ((EditText)findViewById(R.id.EditText14));
arrayOfEditText1[5] = ((EditText)findViewById(R.id.EditText15));
arrayOfEditText1[6] = ((EditText)findViewById(R.id.EditText16));
arrayOfEditText1[7] = ((EditText)findViewById(R.id.EditText17));
arrayOfEditText1[8] = ((EditText)findViewById(R.id.EditText18));
arrayOfEditText1[9] = ((EditText)findViewById(R.id.EditText19));
arrayOfEditText1[10] = ((EditText)findViewById(R.id.EditText20));
for(int i=1;i<=nos;i++)
{
if(arrayOfEditText[i].getText().toString().equals("")||arrayOfEditText1[i].getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(), "Dont leave points empty", 0).show();
}
else
{
calcul();
}
} } public void calcul() { EditText[] arrayOfEditText = new EditText[11];
arrayOfEditText[1] = ((EditText)findViewById(R.id.EditText01));
arrayOfEditText[2] = ((EditText)findViewById(R.id.EditText02));
arrayOfEditText[3] = ((EditText)findViewById(R.id.EditText03));
arrayOfEditText[4] = ((EditText)findViewById(R.id.EditText04));
arrayOfEditText[5] = ((EditText)findViewById(R.id.EditText05));
arrayOfEditText[6] = ((EditText)findViewById(R.id.EditText06));
arrayOfEditText[7] = ((EditText)findViewById(R.id.EditText07));
arrayOfEditText[8] = ((EditText)findViewById(R.id.EditText08));
arrayOfEditText[9] = ((EditText)findViewById(R.id.EditText09));
arrayOfEditText[10] = ((EditText)findViewById(R.id.EditText10));
EditText[] arrayOfEditText1 = new EditText[11];
arrayOfEditText1[1] = ((EditText)findViewById(R.id.EditText11));
arrayOfEditText1[2] = ((EditText)findViewById(R.id.EditText12));
arrayOfEditText1[3] = ((EditText)findViewById(R.id.EditText13));
arrayOfEditText1[4] = ((EditText)findViewById(R.id.EditText14));
arrayOfEditText1[5] = ((EditText)findViewById(R.id.EditText15));
arrayOfEditText1[6] = ((EditText)findViewById(R.id.EditText16));
arrayOfEditText1[7] = ((EditText)findViewById(R.id.EditText17));
arrayOfEditText1[8] = ((EditText)findViewById(R.id.EditText18));
arrayOfEditText1[9] = ((EditText)findViewById(R.id.EditText19));
arrayOfEditText1[10] = ((EditText)findViewById(R.id.EditText20));
for(i=1;i<=nos;i++)
{
//perform calculation
} }
now the logic to be performed in the //perform calculation area is..
arrayofedittext[1]*arrayofedittext1[1] + arrayofedittext[2]*arrayofedittext1[2] + arrayofedittext[3]*arrayofedittext1[3] + etc based on nos / arrayofedittext[1] + arrayofedittext[2] + arrayofedittext[3] + etc based on nos
so can someone help me complete this coding?? 🙂
The less times you perform the same action the faster your app will run. So make
EditText[] arrayOfEditTextandEditText[] arrayOfEditText1class-scoped variables, define them once in onCreate() and you won’t have to use findViewById() onR.id.EditText**after that point.Here’s a function that checks if any of the rows in the calculation are empty, if not you then
totalwill have what you want.Finer Points
Define
arrayOfEditTextandarrayOfEditText1to be visible for the entire class like this:You should know that an array is a zero-index list. So the first entries in your arrays (
arrayOfEditTextandarrayOfEditText1) are null, if you ever try and referencearrayOfEditText[0]expecting an EditText your app will crash.Lastly this code assumes that every EditText is a valid Double, if you don’t restrict non-numeric entries already adding a check is quite simple.