My problem is that I have a method that creates an arraylist. I have no problem passing that around to other methods to be used as needed. However I want to invoke a method calculate when the user clicks the "calculate" button that uses the previously created array list but I don't know how to do it.
The closest I've come is declaring my arraylist as a global variable but I know global variables are generally bad and even then I get a Cannot resolve symbol 'al' on the last line of code lookUpMethod(al, lookUpString);.
I'm not sure what I'm doing wrong or what the best way to proceed is.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String []> al = new ArrayList<>(); //tried global variable
exampleArrayListofArray (al);
}
public void exampleArrayListofArray (List<String []> al) {
//ArrayList<String []> al = new ArrayList<>(); //original array list declaration
al.add(new String[] {"AB","YZ","12"});
al.add(new String[] {"CD","WX", "34"});
al.add(new String[] {"EF","UV", "56"});
al.add(new String[] {"GH","ST", "78"});
al.add(new String[]{"IJ", "QR", "91"});
al.add(new String[]{"KL", "OP", "10"});
displayArrayListofArray(al);
}
public void displayArrayListofArray(List<String[]> al) {
for (String [] row : al)
for (int column = 0; column <= 2 ; column ++){
System.out.println("Value at Index Row " + al.indexOf(row) +
" Column " + column + " is " + (row)[column]);
}
EditText userField = (EditText) findViewById(R.id.user_field);
String lookUpString = userField.getText().toString();
lookUpMethod(al, lookUpString);
}
public void lookUpMethod(List<String[]> al, String lookUpString) {
boolean isStringFound = false;
for (String[] row : al) {
for (int column = 0; column <= 2; column++) {
if (al.get(al.indexOf(row))[column].equals(lookUpString)) {
System.out.println("Index of '" + lookUpString + "': " + al.indexOf(row) + column);
isStringFound = true;
}
}
}
if (!isStringFound) {
System.out.println("Search string '" + lookUpString + "' does not exist.");
}
}
public void calculate(View view){
EditText userField = (EditText) findViewById(R.id.user_field);
String lookUpString = userField.getText().toString();
System.out.println("user input : " + lookUpString);
lookUpMethod(al, lookUpString);
}
}