【问题标题】:Passing an array from class A to class B将数组从 A 类传递到 B 类
【发布时间】:2012-01-05 05:17:40
【问题描述】:

我在 A 类中有一个名为 Floors 的数组,它包含类似以下的值:

  List<Point> manualpoint = new ArrayList<Point>();
  int[] manualpointx = {135,200,300,155,235,300};

假设我希望将这些值传递给 B 类,假设 B 类已经声明了一个超类 View

public class DrawView extends View implements OnTouchListener { 
    @Override
    public void onDraw(Canvas canvas) {
        //pass the values into this class
    }

}

如何将值从 A 类传递到 B?

【问题讨论】:

    标签: java android


    【解决方案1】:

    在活动之间传递数据

    //to pass array use
    intent.putExtra("intarray", manualpointx);
    // to pass list of point use
    intent.putParcelableArrayListExtra("bundle", (ArrayList<? extends Parcelable>) manualpoint);
    

    创建公共 getter 方法以返回值并调用它们 来自您想要获取值的任何类的方法

    【讨论】:

      【解决方案2】:

      你可以这样做。

      final class A
      {
           private static int[] manualpointx = {135,200,300,155,235,300};
           private static List<Point> manualpoint = new ArrayList<Point>();
      
           public static int[] returnArray()
           {
                return(manualpointx);   
           }
      
           public static List<Point> returnList()         
           {
                return(manualpoint);
           }
      
      }
      public class DrawView extends View implements OnTouchListener
      { 
          @Override
          public void onDraw(Canvas canvas) 
          {
               //pass the values into this class
               int arr[]=A.returnArray();
               List<Point> list=A.returnList();
          }
      }
      

      如果您的类 A 中只需要非静态字段,并且您只想使用非静态方法,则必须通过您的类 DrawView 中的类 A 的实例访问它们.

      【讨论】:

      • 谢谢你,这个答案是完美的。你摇滚!干杯队友
      • @Ron Tan:) 在这种情况下,使用static 嵌套类会更合适。
      【解决方案3】:

      你需要创建一个 Class A 的实例,然后访问 Class B 的值,例如:

      public class ClassA {
        private int someInt;
      
        public ClassA() {
          someInt = 5;
        }
      
        public int getSomeInt() {
          return someInt;
        }
      }
      
      public class ClassB {
        public void someMethod() {
          ClassA instanceOfClassA = new ClassA();
      
          int someIntFromClassA = instanceOfClassA.getSomeInt();  //This is now 5;
      
          // Rest of your code here
        }
      }
      

      或者,您可以在ClassA 中将其创建为静态方法,然后使用ClassA.getSomeInt();ClassB 中调用它

      【讨论】:

        【解决方案4】:

        另一个想法是,尝试在 B 中创建一个公共类,如果可能的话,使用 A 的 Array 值调用 B 的 set Array 方法。现在在 set Array 中,我们可以有一个类变量来保存 Array 值,然后调用 draw .

        public class DrawView extends View implements OnTouchListener {
            int[] manualpointx;
            setArray(int[] manualpointx1){
                this.manualpointx = manualpointx1
            }
             public void onDraw(Canvas canvas) { 
                //pass the values into this class 
                we can use manualpointx 
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-10
          • 1970-01-01
          • 2011-06-18
          • 1970-01-01
          • 2011-06-07
          • 2021-12-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多