2015-06-30 23 views
0

我有一個android片段,只有在方向更改時才恢復類對象的最後部分。我的代碼如下:Android部分持久savedInstanceState

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

    if (getArguments().containsKey(ARG_ITEM_ID)) { 
     // Load the weapon content specified by the fragment 
     mWeapon = EquipmentContent.WEAPON_MAP.get(getArguments().getString(ARG_ITEM_ID)); 
    } 
} 

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

    // get reference to the ImageViews 
    weaponImageView = (ImageView) rootView.findViewById(R.id.picture); 
    groupImageView = (ImageView) rootView.findViewById(R.id.groupSymbol); 
    individualImageView = (ImageView) rootView.findViewById(R.id.individualSymbol); 

    // get a reference to the linear layout 
    statLinearLayout = (LinearLayout) rootView.findViewById(R.id.statLinearLayout); 

    if (mWeapon != null) { 

     AssetManager assets = getActivity().getAssets(); 
     InputStream pictureStream; 
     InputStream groupStream; 
     InputStream individualStream; 

     try 
     { 
      // get an InputStream to the asset representing the next picture 
      pictureStream = assets.open("Pictures/" + mWeapon.picture + ".png"); 

      // load the asset as a Drawable and display on the ImageView 
      Drawable picture = Drawable.createFromStream(pictureStream, mWeapon.picture); 
      weaponImageView.setImageDrawable(picture); 
     } 
     catch (IOException e) 
     { 
      Log.e("MainFragment", "Error loading " + mWeapon.picture + ".png", e); 
     } 
     try 
     { 
      // get an InputStream to the asset representing the next picture 
      groupStream = assets.open("Group/" + mWeapon.group + ".png"); 

      // load the asset as a Drawable and display on the ImageView 
      Drawable group = Drawable.createFromStream(groupStream, mWeapon.group); 
      groupImageView.setImageDrawable(group); 
     } 
     catch (IOException e) 
     { 
      Log.e("MainFragment", "Error loading " + mWeapon.group + ".png", e); 
     } 
     try 
     { 
      // get an InputStream to the asset representing the next picture 
      individualStream = assets.open("Individual/" + mWeapon.individual + ".png"); 

      // load the asset as a Drawable and display on the ImageView 
      Drawable individual = Drawable.createFromStream(individualStream, mWeapon.individual); 
      individualImageView.setImageDrawable(individual); 
     } 
     catch (IOException e) 
     { 
      Log.e("MainFragment", "Error loading " + mWeapon.individual + ".png", e); 
     } 
     ((TextView) rootView.findViewById(R.id.item_detail)).setText(mWeapon.id); // Sets the title text of the fragment 
     populateStatistics(inflater); 


    } 
    return rootView; 
} 

public void populateStatistics(LayoutInflater inflater) { 
    // TODO: currently reverts all EditText views to the last input upon orientation change 
    if (!mWeapon.priWeapon.equals("null")) { // if the primary weapon is not null, then inflate relevant TextViews 
     TableLayout priWeaponIDTableLayout = (TableLayout) inflater.inflate(R.layout.statistics_text_view, null); // Weapon title statistic 
     statLinearLayout.addView(priWeaponIDTableLayout); 
     ((TextView) priWeaponIDTableLayout.findViewById(R.id.category)).setText(R.string.priWeapon); 
     ((EditText) priWeaponIDTableLayout.findViewById(R.id.statistic)).setText(String.format("%s", mWeapon.priWeapon)); 
     TableLayout priWeaponRangeTableLayout = (TableLayout) inflater.inflate(R.layout.statistics_text_view, null); // Weapon range statistic 
     statLinearLayout.addView(priWeaponRangeTableLayout); 
     ((TextView) priWeaponRangeTableLayout.findViewById(R.id.category)).setText(R.string.priMaxRange); 
     ((TextView) priWeaponRangeTableLayout.findViewById(R.id.category)).setTypeface(null, Typeface.NORMAL); 
     ((TextView) priWeaponRangeTableLayout.findViewById(R.id.statistic)).setTypeface(null, Typeface.NORMAL); 
     ((EditText) priWeaponRangeTableLayout.findViewById(R.id.statistic)).setText(String.format("%s meters", mWeapon.priMaximumRange)); 
     TableLayout priWeaponPenTableLayout = (TableLayout) inflater.inflate(R.layout.statistics_text_view, null); // Weapon penetration statistic 
     statLinearLayout.addView(priWeaponPenTableLayout); 
     ((TextView) priWeaponPenTableLayout.findViewById(R.id.category)).setText(R.string.priPen); 
     ((TextView) priWeaponPenTableLayout.findViewById(R.id.category)).setTypeface(null, Typeface.NORMAL); 
     ((TextView) priWeaponPenTableLayout.findViewById(R.id.statistic)).setTypeface(null, Typeface.NORMAL); 
     ((EditText) priWeaponPenTableLayout.findViewById(R.id.statistic)).setText(String.format("%s mm", mWeapon.priPenetration)); 
    } 

}

,我膨脹和修改自定義佈局如下。

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="*"> 
    <!-- Table Row 0 --> 
    <TableRow 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:id="@+id/tableRow0"> 
     <TextView 
      android:id="@+id/category" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/priWeapon" android:textColor="#000" android:textStyle="bold" 
      android:layout_weight="1" 
      android:gravity="left" 
      android:padding="5dp"/> 
     <EditText 
      android:id="@+id/statistic" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/NA" android:textSize="14sp" android:textStyle="bold" 
      android:gravity="center" 
      android:focusable="false" 
      android:layout_weight="1" 
      android:cursorVisible="false" 
      android:longClickable="false"/> 
    </TableRow> 
</TableLayout> 

幾乎所有負載罰款時,方位的變化,包括標題,圖片巴紐,集團和個人.pngs,武器ID和武器滲透統計。問題在於武器穿透統計數據由於某種原因取代了主要武器ID和最大射程。我不完全確定爲什麼。任何援助表示讚賞。

回答

0

我修改了我的代碼,並將常見有缺陷的分母視圖類型'EditText'更改爲'TextView'。它現在保留了它應該的數據。出於某種原因,EditText savedInstanceState不是持久的,而是TextView。我最初使用EditText是因爲我使用的Dietel書籍使用了它爲它提供的下劃線的好處。現在我只需創建一個自定義背景來複制下劃線並將其應用於XML。