TableRow中的EditText值变得混乱

时间:2018-06-25 07:16:57

标签: android xamarin.android visual-studio-2017 android-sqlite

我有一个使用ViewPager控件和FragmentPagerAdapter控件的应用程序。

我已经创建了一个TableRow视图,如下所示:

item_row.axml

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp">
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:text="Item"
      android:layout_margin="1dp"
      android:id="@+id/txtRowItemName"
      android:layout_column="0"
      android:layout_weight="3"
      android:gravity="left" />
  <EditText
        android:layout_width="wrap_content"
        android:layout_height="match_parent"        
        android:layout_column="1"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:inputType="number" 
        android:gravity="center"
        android:imeOptions="actionNext"
        android:id="@+id/txtInventory" />
</TableRow>

我在另一视图中有一个TableLayout

inventory.axml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
       android:fillViewport="true"
       android:layout_width="fill_parent"
       android:isScrollContainer="false"
       android:layout_height="0dp"
       android:scrollbars="vertical">
  <TableLayout
    android:layout_marginTop="25dp"
    android:layout_marginLeft="25dp"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:id="@+id/tblInventory"
    android:gravity="top">
  </TableLayout>      
</ScrollView>

这是我的片段的后端代码

public class InventoryFragment : Android.Support.V4.App.Fragment
{
    private DataAccess database;
    private TableLayout _tblLayout;
    private ApiData _data;
    public static InventoryFragment NewInstance(ApiData data, DataAccess dbAccess)
    {
        InventoryFragment newFragment = new InventoryFragment { Arguments = new Bundle(), ApiData = data, database = dbAccess };                        
        return newFragment;
    }
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {                
        view = inflater.Inflate(Resource.Layout.inventory, container, false);
        _tblLayout = view.FindViewById<TableLayout>(Resource.Id.tblInventory);
        IList<StoreItem> storeItems = database.GetStoreItems();
        IList<InventoryCheck> inventoryValues = database.GetLastCheck();
        if(storeItems != null)
        {
            if(storeItems.Any())
            {                       
                foreach (StoreItem itm in storeItems)
                {
                    InventoryCheck lastInventory = null;
                    if (inventoryValues != null)
                        lastInventory = inventoryValues.SingleOrDefault(i => i.ItemID == itm.ID);
                    View itemRows = inflater.Inflate(Resource.Layout.item_row, container, false);
                    _tblLayout.AddView(itemRows);
                    TextView txtItemName = itemRows.FindViewById<TextView>(Resource.Id.txtRowItemName);
                    //Setup edit text
                    EditText txtInventory = itemRows.FindViewById<EditText>(Resource.Id.txtInventory);  
                    txtInventory.Tag = itm.ID;              
                    //Add OnFocus Changed
                    txtInventory.FocusChange += (sender, e) => 
                    {
                        if(!e.HasFocus)
                        {
                            int value = 0;
                            int.TryParse(txtInventory.Text, out value);
                            //Saves the value to database upon loosing focus
                            database.SetInventoryItem(itm.ID, value);
                        }
                    };                  
                    txtItemName.Text = itm.ItemName;
                    txtInventory.Text = (lastInventory) != null && lastInventory.Inventory > 0 ? lastInventory.Inventory.ToString() : string.Empty;
                }

            }

        }
    }
}

我面临的问题是,当我在字段中放置文本时,它将文本保存在数据库中。调试后,我可以看到数据传输正常,但是在加载视图时,所有值都被弄乱了。它将所有值替换为最后一项中的值。

当我将应用程序从“最近的项目”中滑出并再次运行时,填充它们的值是正确的。我在做什么错了。

0 个答案:

没有答案