Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.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
Android 将数组从活动传递到视图_Android_View_Android Activity - Fatal编程技术网

Android 将数组从活动传递到视图

Android 将数组从活动传递到视图,android,view,android-activity,Android,View,Android Activity,我正在为android编写一个应用程序。如何将int数组从活动传递到视图类?我已经搜索过了,但没有找到任何似乎能回答我问题的东西。我有这门课: 公共类ConvertToGrid扩展活动{…} 它从主活动的布局中获取用户的输入(使用意图)并将其转换为int数组:int[]binary={…},该数组有64个值。我该如何将其融入其中: 公共类DrawGrid扩展视图{…} 我天真地尝试了一个意图,但除非我做错了,否则这似乎是一个错误的想法!另外,我假设我不需要像执行活动一样在清单中声明我的视图?试试

我正在为android编写一个应用程序。如何将int数组从活动传递到视图类?我已经搜索过了,但没有找到任何似乎能回答我问题的东西。我有这门课:

公共类ConvertToGrid扩展活动{…}

它从主活动的布局中获取用户的输入(使用意图)并将其转换为int数组:
int[]binary={…}
,该数组有64个值。我该如何将其融入其中:

公共类DrawGrid扩展视图{…}

我天真地尝试了一个意图,但除非我做错了,否则这似乎是一个错误的想法!另外,我假设我不需要像执行活动一样在清单中声明我的视图?

试试这个

public class ConvertToGrid extends Activity{.

Public int[] binary = {...}

public class DrawGrid extends View{...}

..}

创建一个具有常用参数和int数组的构造函数,例如:

public class DrawGrid extends View {

    private int[] intArray;

    public DrawGrid(Context context,int[] intArray) {
        super(context);
        // TODO Auto-generated constructor stub
            this.intArray = intArray;
    }

    public DrawGrid(Context context, AttributeSet attrs, int defStyle,int[] intArray) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
            this.intArray = intArray;
    }

    public DrawGrid(Context context, AttributeSet attrs,int[] intArray) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
            this.intArray = intArray;
    }


}

只需创建一个具有param int[]的构造函数

public DrawGrid(int[] binaries)
{
    // constructor
} 

在视图中封装数据(如果它正在使用数据)可能比在活动中封装好。通过这种方式,您可以在不同的活动中轻松地重用它。这是否只是使int数组对我项目中的所有类公开可用?再一次,另一个非常简单的选择。这对我来说似乎是个好主意。我现在意识到我的问题是多么基本。快速跟进,然后我如何使用我传递的数组?我试图在以后的代码中引用它,但它抱怨它不知道什么是“intArray”。@David请再次查看我的答案。在类中创建一个字段并在构造函数中设置它就足够了。我现在意识到我在做什么,我需要从构造函数中的数组中获取我的值,否则他们会在数组被交给类之前查找它。
public DrawGrid(int[] binaries)
{
    // constructor
}