2013-10-04 43 views
0

我試圖保存從我的3個片段中的每個EditText中輸入的所有信息。我試圖從ActionBar中的選項按鈕中獲取這些信息。無法從FragmentActivity中找到EditText

我注意到,當我在不同的選項卡上時,它無法找到視圖。

我的第一片段/第一個選項卡:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:tag="gen_info"> 

<LinearLayout android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="vertical"> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Admin Account" 
     android:id="@+id/isAdmin" /> 

    <EditText 
     android:id="@+id/code" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_marginTop="5dip" 
     android:singleLine="true" 
     android:hint="@string/code" 
     android:ems="10" 
     android:width="150dip" /> 

    <EditText 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:singleLine="true" 
     android:hint="@string/fullname" /> 

    <EditText 
     android:id="@+id/address" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:hint="@string/address" 
     android:layout_marginTop="5dip" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:inputType="date" 
     android:ems="10" 
     android:id="@+id/birthdate" 
     android:hint="Birthdate (mm/dd/yyy)" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:inputType="number" 
     android:ems="10" 
     android:id="@+id/mobile" 
     android:hint="Mobile No" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="number" 
     android:layout_marginTop="5dip" 
     android:ems="10" 
     android:id="@+id/workPhone" 
     android:hint="Work Phone" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:inputType="textEmailAddress" 
     android:ems="10" 
     android:id="@+id/email" 
     android:hint="Email" /> 

     <Spinner 
      android:id="@+id/gender_options" 
      android:layout_marginTop="5dip" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

    <Spinner 
     android:id="@+id/civil_status" 
     android:layout_marginTop="5dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <EditText 
     android:layout_marginTop="5dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="date" 
     android:ems="10" 
     android:id="@+id/hired_date" 
     android:hint="Hired Date (mm/dd/yyyy)" /> 

    <EditText 
     android:id="@+id/location" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:singleLine="true" 
     android:hint="Location" /> 

    <Spinner 
     android:id="@+id/position" 
     android:layout_marginTop="5dip" 
     android:layout_marginBottom="5dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <Spinner 
     android:id="@+id/department" 
     android:layout_marginTop="5dip" 
     android:layout_marginBottom="5dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

我的第二片段/標籤

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:tag="contacts"> 

<LinearLayout android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="vertical"> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:id="@+id/contactName" 
     android:hint="Name" 
     android:paddingTop="5dip" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:id="@+id/contactRelationship" 
     android:hint="Relationship" 
     android:paddingTop="5dip" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPostalAddress" 
     android:ems="10" 
     android:id="@+id/contactAddress" 
     android:hint="Address" 
     android:paddingTop="5dip" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="phone" 
      android:ems="10" 
      android:id="@+id/contactPhoneNo" 
      android:hint="Phone Number" 
      android:paddingTop="5dip" /> 

     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/contactPhoneTypeSpinner" 
      android:layout_toEndOf="@+id/contactPhoneNo" 
      android:layout_toRightOf="@+id/contactPhoneNo" 
      android:paddingTop="5dip" /> 
    </RelativeLayout> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="107dp" 
     android:inputType="textMultiLine" 
     android:ems="10" 
     android:id="@+id/contactSpecialNotes" 
     android:paddingTop="5dip" 
     android:hint="Write special notes here" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add to list" 
     android:id="@+id/addContact" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Contacts List" 
     android:id="@+id/contactsHeader" 
     android:editable="false" /> 

    <TableLayout 
     android:id="@+id/contactsTableLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:paddingTop="5dip"></TableLayout> 
</LinearLayout> 

我3rdfragment /選項卡:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:tag="latest_wage"> 

<LinearLayout android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="vertical"> 


    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="date" 
     android:ems="10" 
     android:id="@+id/latestWageDate" 
     android:hint="Date" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" 
     android:ems="10" 
     android:id="@+id/latestWageNote" 
     android:hint="Note" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/latestWageRate" 
     android:hint="Rate (PHP)" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add Rate" 
     android:id="@+id/btnAddRate" /> 

    <TableLayout 
     android:id="@+id/latestWageTableLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingTop="5dip"></TableLayout> 
</LinearLayout> 

我宣佈我的第一個分片的元素在這裏:

public static class LaunchpadSectionFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false); 

     Spinner spinner = (Spinner) rootView.findViewById(R.id.gender_options); 
     Spinner civilStatusSpinner = (Spinner) rootView.findViewById(R.id.civil_status); 
     Spinner positionSpinner = (Spinner) rootView.findViewById(R.id.position); 
     Spinner departmentSpinner = (Spinner) rootView.findViewById(R.id.department); 

     return rootView; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 
} 

下面是我動作條功能:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      this.finish(); 
      return true; 
     case R.id.action_save: 

      EditText code = (EditText) findViewById(R.id.code); 
      EditText name = (EditText) findViewById(R.id.name); 
      EditText address = (EditText) findViewById(R.id.address); 
      EditText birthDate = (EditText) findViewById(R.id.birthdate); 
      EditText mobile = (EditText) findViewById(R.id.mobile); 
      EditText workPhone = (EditText) findViewById(R.id.workPhone); 
      EditText email = (EditText) findViewById(R.id.email); 
      Spinner gender = (Spinner) findViewById(R.id.gender_options); 
      Spinner civilStatus = (Spinner) findViewById(R.id.civil_status); 
      EditText hiredDate = (EditText) findViewById(R.id.hired_date); 
      EditText location = (EditText) findViewById(R.id.location); 
      Spinner departmentId = (Spinner) findViewById(R.id.department); 
      CheckBox isAdmin = (CheckBox) findViewById(R.id.isAdmin); 

      return true; 
     case R.id.action_discard: 
      this.finish(); 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

所有的意見將返回null,這些觀點是在第一次發現分段。當我沒有製作兩個碎片時,我可以找到意見。

任何幫助將不勝感激。謝謝!

+0

聲明元素應放置在oncreate或onCreateView – deadfish

+0

您必須在'onCreateView'中擴充佈局,然後才能「查找」所有視圖。 – yugidroid

+0

我確實聲明瞭我的Fragment的onCreateView方法中的元素,但是當我單擊ActionBar按鈕時,我想訪問這些元素的值(來自所有片段)。 – shifter

回答

0

在辦法可以處理你的itemMenuClick在我們的片段本身。所以,你可以在你的片段處理他們,並得到您需要的信息:TUTORIAL

另一種方法是讓所有的片段中的一個實例在MainActivity全球,並要求您的片段的值,例如:

MainActivity: 

Fragment1 _fragment1; 
onCreate(..){ 
    _fragment1 = new Fragment1(); 
} 

onMenuItemSelected(....){ 

    String test = _fragment1.getCustomText(); 
} 


Fragment1: 

EditText _test; 

onCreate(...){ 

    _test = _root.findViewById(R.id.et_test); 
} 


public String getCustomText(){ 
    return _test.getText().toString(); 
} 
+0

非常感謝這個想法,我沒有使用你的代碼,我在我的片段的類中創建了一個靜態變量來保存視圖。 – shifter

0

儘量給出代碼

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      this.finish(); 
      return true; 
     case R.id.action_save: 

      EditText code = (EditText) getView().findViewById(R.id.code); 
      EditText name = (EditText) getView().findViewById(R.id.name); 
      EditText address = (EditText) getView().findViewById(R.id.address); 
      EditText birthDate = (EditText) getView().findViewById(R.id.birthdate); 
      EditText mobile = (EditText) getView().findViewById(R.id.mobile); 
      EditText workPhone = (EditText) getView().findViewById(R.id.workPhone); 
      EditText email = (EditText) getView().findViewById(R.id.email); 
      Spinner gender = (Spinner) getView().findViewById(R.id.gender_options); 
      Spinner civilStatus = (Spinner) getView().findViewById(R.id.civil_status); 
      EditText hiredDate = (EditText) getView().findViewById(R.id.hired_date); 
      EditText location = (EditText) getView().getView().findViewById(R.id.location); 
      Spinner departmentId = (Spinner) getView().findViewById(R.id.department); 
      CheckBox isAdmin = (CheckBox) getView().findViewById(R.id.isAdmin); 

      return true; 
     case R.id.action_discard: 
      this.finish(); 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

謝謝,但我已經試過了...... – shifter