2015-09-05 65 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讀取該信息。每個循環使用一個簡單的。

相關問題