简体   繁体   English

设备方向更改时,充气物品消失了

[英]Inflated items are gone when device orientation changes

Hi i'm using eclipse for deveoping this android app. 嗨,我正在使用Eclipse开发此android应用程序。 I'm inflating a layout using inflator and everything works fine.. I'm adding those inflated items one by one to another layout, so when the device's orientation is changed, all those inflated items are gone. 我正在使用充气机充气一个布局,并且一切正常。.我正在将那些充气物品一个接一个地添加到另一个布局中,因此当设备的方向改变时,所有这些充气物品都消失了。 The application seems like it has been restarted. 该应用程序似乎已重新启动。 But some values are still remaining.. Please help.. 但是一些值仍然保留..请帮助..

public void addNewItem() {
    LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item = li.inflate(R.layout.activity_order_items, null);
    orderItemFrameView.add(item);       
    itemsLayout.addView(item);
    setAdditionalFonts(item);           
}

private void setAdditionalFonts(View view) {        
    TextView lblNameTak = (TextView)view.findViewById(R.id.tak_itemNo); 
    AutoCompleteTextView txtNameTak = (AutoCompleteTextView)view.findViewById(R.id.tak_itemName);   
    ImageView barScan = (ImageView)view.findViewById(R.id.barcode_scan);
    TextView lblRate = (TextView)view.findViewById(R.id.tak_lblRate);
    TextView lblQty = (TextView)view.findViewById(R.id.tak_lblQty);
    TextView lblTotal = (TextView)view.findViewById(R.id.tak_lblTotal);
    TextView rate = (TextView)view.findViewById(R.id.tak_Rate);
    EditText2 qty = (EditText2)view.findViewById(R.id.tak_Qty);
    TextView total = (TextView)view.findViewById(R.id.tak_Total);
    qty.totTextView = total;
    qty.rateTextView = rate;        
    qty.orderTake = this;
    qty.index = orderItemFrameView.size() - 1;
    qty.sqlDb = SqlDb;
    qty.itemName = txtNameTak;
    setupBarcodeScan(barScan);

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/ubuntu-l.ttf");
    Typeface tf2 = Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf");
    Typeface tf3 = Typeface.createFromAsset(getAssets(), "fonts/ubu-r.ttf");        
    Typeface tf4 = Typeface.createFromAsset(getAssets(), "fonts/tahoma.ttf");
    lblNameTak.setTypeface(tf3);        
    txtNameTak.setTypeface(tf2);
    lblRate.setTypeface(tf2);   
    lblQty.setTypeface(tf2);    
    lblTotal.setTypeface(tf2);      
    lblNameTak.setText("Item " + (qty.index + 1));

    DbAdapterItem dbItem;
    TextAdapterItem txtItem;

    dbItem = new DbAdapterItem(this, SqlDb);                
    txtItem = new TextAdapterItem(dbItem,  this,  qty);
    txtNameTak.setAdapter(txtItem);     
    txtNameTak.setOnItemClickListener(txtItem);

    txtNameTak.requestFocus();
}

all these items created by addNewItem() are vanished... addNewItem()创建的所有这些项目都消失了...

here is the layout from which i'm inflating... 这是我正在膨胀的布局...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom ="22dp"

tools:context=".OrderItems" >

<FrameLayout
    android:layout_width="match_parent"        
    android:background= "#888888"
    android:layout_height="98dp" >
    <TextView
        android:id="@+id/tak_itemNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"
        android:layout_marginTop="2.7dp"
        android:text="Item 1"
        android:layout_gravity="right"
        android:textColor="#DDDDDD" />
    <LinearLayout
        android:layout_width="match_parent" 
        android:layout_marginTop="22dp"
        android:orientation="vertical"
        android:background= "#FFFFFF"     
        android:layout_height="75dp" >

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

            <AutoCompleteTextView
                android:id="@+id/tak_itemName"
                android:imeOptions="actionUnspecified"
                android:imeActionLabel="search"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="0dp"
                android:layout_weight="1.06"
                android:completionThreshold="1"
                android:ems="10"
                android:hint="Item Name / Barcode"                  
                android:inputType="textCapWords"
                android:singleLine="true"
                android:textColorHint="#DDDDDD" />

            <ImageView
                android:id="@+id/barcode_scan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="7dp"
                android:paddingRight="7dp"
                android:paddingTop="6dp"
                android:src="@android:drawable/ic_menu_camera" />

        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <FrameLayout
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"

                >
            <TextView
                android:id="@+id/tak_lblRate"
                android:layout_width="wrap_content"
                android:layout_marginLeft="7dp"
                android:layout_height="wrap_content"    
                android:textColor="#555555"
                android:textSize="10sp"            
                android:text="Rate" />

            <TextView
                android:id="@+id/tak_Rate"
                android:layout_width="match_parent"
                android:layout_marginTop="12dp"
                android:layout_marginRight="15dp"
                android:layout_height="wrap_content"    
                android:gravity="right"
                android:textSize="16sp"            
                android:text="0.00" />

            </FrameLayout>

             <FrameLayout
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"

                >
            <TextView
                android:id="@+id/tak_lblQty"
                android:layout_width="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginRight="7dp"
                android:layout_height="wrap_content"    
                android:textColor="#555555"
                android:textSize="10sp"            
                android:text="Qty" />

            <com.sayka.ordergadget.EditText2
                android:id="@+id/tak_Qty"
               android:layout_width="match_parent"
               android:layout_marginBottom="-4dp"
                android:layout_marginTop="4dp"
                android:inputType="numberDecimal"
                android:imeOptions="actionNext"
                android:layout_marginLeft="12dp"
                android:layout_height="wrap_content"    
                android:gravity="center"
                android:textSize="16sp"            
                android:text="0.0" />

            </FrameLayout>

              <FrameLayout
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"

                >
            <TextView
                android:id="@+id/tak_lblTotal"
                android:layout_width="wrap_content"
                android:layout_marginLeft="7dp"
                android:layout_gravity="left"
                android:layout_height="wrap_content"    
                android:textColor="#555555"
                android:textSize="10sp"            
                android:text="Total" />

            <TextView
                android:id="@+id/tak_Total"
               android:layout_width="match_parent"
                android:layout_gravity="right"
                android:layout_marginTop="12dp"
                android:layout_marginRight="15dp"
                android:layout_height="wrap_content"    
                android:gravity="right"
                android:textSize="16sp"            
                android:text="0.00" />

            </FrameLayout>

        </FrameLayout>

    </LinearLayout>



</FrameLayout>

It's normal behaviour and you are correct. 这是正常行为,您是正确的。 the activity is recreated from scratch on rotation. 该活动是从头开始重新创建的。

If you want something to persist. 如果您想坚持下去。 store it during onSaveInstanceState in the provided bundle. 在onSaveInstanceState期间将其存储在提供的捆绑软件中。

then during onCreate check to see if the bundle is null, if it isn't, pull your data out and add the view elements again. 然后在onCreate期间检查捆绑包是否为null,如果不是,则将数据拉出并再次添加view元素。

Alternatively you can fudge it and state in the manifest that your activity will handle orientation/configuration changes. 或者,您可以捏造它并在清单中声明您的活动将处理方向/配置更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM