Airfix Airfix - 5 months ago 14
Java Question

How to pass arraylist into a method from onClick

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);

}
}

Answer

Declare ArrayList<String []> al = new ArrayList<>(); outside the onCreate function.

I really don't see how you'd have problems this way because, all functions would now see your variable. As a clean up, you can remove the variable al when you call functions as it is no longer necessary as every function can now see it. If am I not mistaken, your final code would look like:

public class MainActivity extends AppCompatActivity {

    ArrayList<String []> al = new ArrayList<>(); //declare it here


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        exampleArrayListofArray ();

    }

    public void exampleArrayListofArray () {

        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();
    }

    public void displayArrayListofArray() {

        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(lookUpString);

    }

    public void lookUpMethod(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(lookUpString);

    }
}

Kindly note how I removed "al" in your function calls and in your function parameters. The problem with having al as a local variable that gets passed through functions is that when you hit the calculate button, the calculate function does NOT see al unless it's a global variable.

Go try that revised code.