2

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

    }
}
2
  • You want pass data to another activity ?. Commented Jun 30, 2016 at 2:48
  • I already read your code and don't see al define in method calculate. If you want use it, you can define in variable global Commented Jun 30, 2016 at 2:52

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Doh! I thought I had declared al in the main. facepalm I didn't realize I declared it in the onCreate method.
Don't forget to remove al in the function calls and declaration to avoid redundancy and unnecessary code! It would be readable and "elegant" that way! :)
Works perfectly now. Why is it that I read global variables are bad news? Would there be another way to do this without using a global variable and perhaps being able to pass al to another activity. (I might want to do that in future versions of my app).
@Airfix Since global variables are accessible to every function in your activity, it may become difficult to track which functions use your variable and it's difficult to figure out which functions modifies your variable when your activity becomes large.
However, in your case, global variables won't be much of an issue since you don't have much code yet.
|
3

Global variable should be define outside onCreate and i think it's not generally bad, you can try this code:

public class MainActivity extends AppCompatActivity {

    private ArrayList<String []> al; //global variable in class

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

        al = new ArrayList<>();
        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);

    }
}

3 Comments

That's not a global variable. It is just an instance variable
Yes, like i comment, this is just global variable in class. That means it can access in this class, but can't access from another class. Maybe it's called instance variable :)
My mistake. I thought I had declared it as a global variable facepalm
2

You need to take declation of arrayList outside the function as Global variable.

ArrayList al = new ArrayList<>();

Comments

2

Scope: Local variables are visible only in the method or block they are declared whereas instance variables can been seen by all methods in the class. Place where they are declared: Local variables are declared inside a method or a block whereas instance variables inside a class but outside a method.

You need to Declare

ArrayList<String []> al = new ArrayList<>();

outside the onCreate function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.