2015-09-05 60 views
0

当用户点击包含在我的主Activity中的SurfaceView时,我将项目存储到ArrayList中。从SurfaceView子类传递ArrayList到Activity

主要活动XML

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 

    <newProject.SurfaceClass 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:id="@+id/spaceField" /> 


</LinearLayout> 

onTouch在SurfaceClass

@Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 

     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) 
     { 
      int touchX = (int)(motionEvent.getX()); 
      int touchY = (int)(motionEvent.getY()); 
      Point point = new Point(touchX, touchY); 

      mItemsClicked.add(point); 

      for (int i = mItems.size()-1; i >= 0; i--) 
      { 
       Item item = listOfItems.get(i); 

       inventory.add(item); 



      } 
      invalidate(); 

     } 
     return false; 
    } 

我怎么会去能够使用这种库存的ArrayList我的主要活动中,为了话传递的ArrayList,这样我能够简单地从我的活动中点击一个按钮,并且例如记录该ArrayList控制台。

+0

您是否已将Surface类定义为Activity类的内部类? –

+0

我有Surface类,因为它是自己的类 – Jenna25

回答

0

你可以在返回列表

getMyList().... 

面类,并在您Mainactivity添加一个公共方法..使用由ID找到以获得表面的类的对象

mySurfaceView= findVi.... 

然后...

mySurfaceView.getMyList(); 
0

视图在活动中。为什么不在活动中创建一个全局成员变量ArrayList,您的SurfaceView可以访问它并填充它,然后在需要时从Activity读取该信息。每个循环使用一个简单的。

相关问题