2013-04-01 18 views
0

在我的Android應用程序,以獲取來自不同的片段變量,我有4個片段的活動。所有的片段都有相同的確切字段,所以它們都從相同的xml文件中膨脹並具有相同的java類。 但是,我需要獲取存儲在其中每個數據中的數據,因爲用戶將在每個數據中輸入內容。那麼,如果他們都擁有相同的課程,我將如何獲得數據呢?如何使用同一類

activity_success.xml

<Button 
    android:id="@+id/continue_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/cont" 
    /> 

<Button 
    android:id="@+id/export_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/export" 
    android:layout_toRightOf="@id/continue_button" 
    /> 


<fragment android:name="org.bah.bu.SuccessFragment" 
    android:id="@+id/red1_success" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/continue_button" 

    /> 

<fragment android:name="org.bah.bu.SuccessFragment" 
    android:id="@+id/red2_success" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/red1_success" 
    /> 

fragment_success.xml

<CheckBox 
    android:id="@+id/far" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/far" 
    /> 

<CheckBox 
    android:id="@+id/lifted" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/lifted" 
    /> 

+0

發表您的代碼 - – minhaz

+0

@minhaz貼吧 – rasen58

回答

0

沒關係,我理解了它。我使用getSupportFragmentManager().findFragmentById(R.id.frag);方法創建4個引用片段,並使用updateVals()方法將片段中的視圖數據存儲在java類中的變量中。

1

寫的片段類getData()方法(例如,它返回的相關信息),或簡單地使用標準的getter你需要的任何數據字段。然後,在您的封閉活動中,調用每個片段實例的方法getData()。每個實例將返回它所包含的對該實例唯一的數據。

重要的是,無論這些碎片共享同一個類定義的事實,它們的實例將(或可以)包含不同的數據。談論實例而不是課堂是解決方案。

+0

謝謝,我已經有了這一點,但我只是缺少'getSupportFragmentManager()findFragmentById(R.id.frag);' – rasen58

相關問題