Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将arraylist从onClick传递到方法中_Java_Android_Arraylist_Methods_Onclick - Fatal编程技术网

Java 如何将arraylist从onClick传递到方法中

Java 如何将arraylist从onClick传递到方法中,java,android,arraylist,methods,onclick,Java,Android,Arraylist,Methods,Onclick,我的问题是我有一个创建arraylist的方法。我可以根据需要将其传递给其他方法。但是,当用户单击使用先前创建的数组列表的“计算”按钮时,我想调用一个方法calculate,但我不知道如何操作 最近的一次是将我的arraylist声明为全局变量,但我知道全局变量通常是不好的,即使在代码lookUpMethod(al,lookUpString)的最后一行,我也得到了一个无法解析符号'al' 我不确定我做错了什么,也不确定最好的方法是什么 public class MainActivity exte

我的问题是我有一个创建arraylist的方法。我可以根据需要将其传递给其他方法。但是,当用户单击使用先前创建的数组列表的“计算”按钮时,我想调用一个方法
calculate
,但我不知道如何操作

最近的一次是将我的arraylist声明为全局变量,但我知道全局变量通常是不好的,即使在代码
lookUpMethod(al,lookUpString)的最后一行,我也得到了一个
无法解析符号'al'

我不确定我做错了什么,也不确定最好的方法是什么

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

    }
}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList al=new ArrayList();//尝试使用全局变量
例如Arraylistofarray(al);
}
公共无效示例ArrayListFarray(列表al){
//ArrayList al=new ArrayList();//原始数组列表声明
al.add(新字符串[]{“AB”、“YZ”、“12”});
al.add(新字符串[]{“CD”、“WX”、“34”});
添加(新字符串[]{“EF”、“UV”、“56”});
al.add(新字符串[]{“GH”、“ST”、“78”});
添加(新字符串[]{“IJ”、“QR”、“91”});
al.add(新字符串[]{“KL”,“OP”,“10”});
显示阵列式半导体法拉利(al);
}
公共void displayListoFarray(列表al){
对于(字符串[]行:al)

对于(int column=0;column全局变量应该在onCreate外部定义,我认为这通常并不坏,您可以尝试以下代码:

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

    }
}
public类MainActivity扩展了AppCompatActivity{
private ArrayList al;//类中的全局变量
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
al=新的ArrayList();
例如Arraylistofarray(al);
}
公共无效示例ArrayListFarray(列表al){
//ArrayList al=new ArrayList();//原始数组列表声明
al.add(新字符串[]{“AB”、“YZ”、“12”});
al.add(新字符串[]{“CD”、“WX”、“34”});
添加(新字符串[]{“EF”、“UV”、“56”});
al.add(新字符串[]{“GH”、“ST”、“78”});
添加(新字符串[]{“IJ”、“QR”、“91”});
al.add(新字符串[]{“KL”,“OP”,“10”});
显示阵列式半导体法拉利(al);
}
公共void displayListoFarray(列表al){
对于(字符串[]行:al)
对于(int column=0;columnDeclare
ArrayList al=new ArrayList();
onCreate
函数之外

我真的不明白这样会有什么问题,因为所有函数现在都会看到您的变量。作为清理,您可以在调用函数时删除变量
al
,因为它不再是必需的,因为每个函数现在都可以看到它。如果我没有弄错的话,您的最终代码如下所示:

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

    }
}
public类MainActivity扩展了AppCompatActivity{
ArrayList al=new ArrayList();//在此处声明
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
例如ArrayListoFarray();
}
公共无效示例ArrayListoFarray(){
al.add(新字符串[]{“AB”、“YZ”、“12”});
al.add(新字符串[]{“CD”、“WX”、“34”});
添加(新字符串[]{“EF”、“UV”、“56”});
al.add(新字符串[]{“GH”、“ST”、“78”});
添加(新字符串[]{“IJ”、“QR”、“91”});
al.add(新字符串[]{“KL”,“OP”,“10”});
displayArrayListofArray();
}
public void displayArrayListofArray(){
对于(字符串[]行:al)

对于(int column=0;column您需要将函数外部的arrayList的degration作为全局变量


ArrayList al=new ArrayList();

作用域:局部变量仅在声明它们的方法或块中可见,而实例变量可以被类中的所有方法看到。声明它们的位置:局部变量在方法或块内声明,而实例变量在类内但在方法外声明

你需要申报

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

在onCreate函数外部。

是否要将数据传递给另一个活动?。我已经阅读了您的代码,没有在方法calculate中看到所有定义。如果您想使用它,可以在变量GlobalT中定义它,它不是全局变量。它只是一个实例变量。是的,就像我的注释一样,这只是类中的全局变量。这意味着它可以在中访问此类,但无法从其他类访问。可能称为实例变量:)我的错误。我以为我已将其声明为全局变量facepalmDoh!我以为我已在main中声明了al。facepalm我没有意识到我已在onCreate方法中声明了它。别忘了在函数调用和声明中删除
al
,以避免冗余和不必要的代码!这样做既可读又“优雅!”:)现在工作得很好。为什么我读到全局变量是坏消息?有没有其他方法可以做到这一点,而不使用全局变量,或者可以将al传递给其他活动。(我可能希望在我的应用程序的未来版本中这样做)@Airfix由于活动中的每个函数都可以访问全局变量,因此可能很难跟踪哪些函数使用了您的变量,也很难在活动变大时确定哪些函数修改了您的变量。但是,在您的情况下,全局变量不会成为一个大问题,因为您没有太多的合作伙伴还没有。