HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Three edittext boxes: suggestions to clean up code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
threeboxesedittextcodesuggestionsclean

Problem

I am a newbie at writing Android apps and using Java in general. I have went through most of the Android hello view tutorials but still seem to be lacking some understanding of the basics. Here is a snippet of code I wrote for an app. My goal was to have three edittext boxes for the user to input information. I want the user to put information in two of the three boxes and when they hit the calculate button, it will calculate the third edittext value based on a specific equation. Is there anyway that I can avoid initializing these 3 edittext objects in each of my methods within this class? Any other suggestions for cleaning up or improving this code?

```
public class PlantPopulation extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

EditText seedSpacing = (EditText)
findViewById(R.id.seedSpacing);
EditText rowSpacing = (EditText)
findViewById(R.id.rowSpacing);
EditText population = (EditText)
findViewById(R.id.population);

seedSpacing.setText(""); //clear values
rowSpacing.setText("");
population.setText("");

}

//check how many boxes are empty
public int checkifempty(String array[]) {

int i = 0;
for (String string : array) {
if (string.equals("")) {
i = i+1;
}
}
return i;
}

//run the calculation
public void calc(View v) {

EditText seedSpacing = (EditText)
findViewById(R.id.seedSpacing);
EditText rowSpacing = (EditText)
findViewById(R.id.rowSpacing);
EditText population = (EditText)
findViewById(R.id.population);

String sS = seedSpacing.getText().toString();
String rS = rowSpacing.getText().t

Solution


  • Replace "43560*144" by a constant.



  • Store new Double(sS), new Double(srS), new Double(pop) in variables


before if (checkifempty(boxes)

  • Correct indentation in the onCreate` method

Context

StackExchange Code Review Q#5998, answer score: 3

Revisions (0)

No revisions yet.