为什么不保存当前的EditText值?

时间:2014-06-04 06:48:07

标签: c# android android-edittext xamarin

我有多个EditTexts的ListView,EditTexts由用户填写。当我填充EditTexts并关闭键盘时,不会保存新的EditTexts值并显示旧值。所有属性都是默认属性。我正在真实设备SGS3上调试应用程序。

我的ListView适配器:

public class OrderListViewAdapter :  BaseAdapter<Product>
{
    private readonly Activity _context;
    public string _uLogin;
    public string _uPass;
    public string _routeId;
    public string _groupId;

    List<Product> Products = new List<Product> ();

    public OrderListViewAdapter (Activity context,UserInfo uInfo,string routeId,string groupId)
    {
        _context = context;
        _routeId = routeId;
        _groupId = groupId;


        foreach (TradepointInfo tInfo in uInfo.Tradepoint) {

            if ( Int32.Parse(tInfo.Id).ToString() == routeId  ) {
                foreach (Product p in tInfo.Products) {
                    if (p.Group == groupId) {
                        Products.Add (new Product {
                            ID = p.ID,
                            Name = p.Name,
                            Route = p.Route,
                            Quantity = p.Quantity,
                            Price = p.Price,
                            Group = p.Group
                        });
                    }
                }


            }

        }
    }


    public override int Count
    {
        get{ return Products.Count;}
    }

    public override long  GetItemId(int position)
    {
        return  Int64.Parse( Products[position].ID);
    }

    public override   Product this[int position]
    {
        get{return  Products[position];}
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
            view = _context.LayoutInflater.Inflate(Resource.Layout.OrderListItem, null);



        view.FindViewById<TextView> (Resource.Id.productNameTextView).Text = Products [position].Name;
        view.FindViewById<EditText> (Resource.Id.quantityEditText).Text = Products[position].Quantity;

        return view;
    }

}

OrderList.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/productSearchEditText"
        android:textSize="18sp"
        android:visibility="gone" />
    <TextView
        android:text="Route Name"
        android:layout_width="match_parent"
        android:layout_height="42.7dp"
        android:id="@+id/ordRouteNameTextView"
        android:textSize="18sp"
        android:layout_marginLeft="5dp" />
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/orderListView" />
</LinearLayout>

OrderListItem.axml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="match_parent"
    p1:layout_height="wrap_content"
    p1:id="@+id/orderRelativeLayout">
    <TextView
        p1:text="Product Name"
        p1:layout_width="227.9dp"
        p1:layout_height="63.0dp"
        p1:id="@+id/productNameTextView"
        p1:layout_marginLeft="5dp"
        p1:textSize="18sp"
        p1:layout_marginBottom="14.6dp"
        p1:layout_marginRight="23.6dp" />
    <EditText
        p1:layout_width="50.9dp"
        p1:layout_height="wrap_content"
        p1:layout_toRightOf="@id/productNameTextView"
        p1:id="@+id/quantityEditText"
        p1:layout_marginRight="0.0dp"
        p1:layout_marginLeft="6.7dp" />
</RelativeLayout>

OrderActivity:

 public class OrderActivity : Activity {
 string projectDir = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path,"StarSoftMobile");

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.OrderList);

            string routeId = Intent.GetStringExtra ("RouteId") ?? "Empty";
            string routeName = Intent.GetStringExtra ("RouteName") ?? "Empty";
            string groupId = Intent.GetStringExtra ("GroupId") ?? "Empty"; 
            string groupName = Intent.GetStringExtra ("GroupName") ?? "Empty"; 


            ListView _orderListView = FindViewById<ListView> (Resource.Id.orderListView);
            TextView _ordRouteNameTextView = FindViewById<TextView> (Resource.Id.ordRouteNameTextView);
            _ordRouteNameTextView.Text = routeName;

            string uInfoFromDevice = File.ReadAllText (projectDir + "/" + "Routes.json");

            UserInfo uInfo = JsonConvert.DeserializeObject<UserInfo> (uInfoFromDevice);

            OrderListViewAdapter _adapter = new OrderListViewAdapter (this,uInfo,routeId,groupId);
            _adapter.NotifyDataSetChanged ();
            _orderListView.Adapter = _adapter; 

        }
    }

1 个答案:

答案 0 :(得分:0)

您的编辑文字是列表视图项目中的视图 编辑时需要将其保存到模型中(适配器的数据) 因此,您需要在适配器的getView()方法中保存和更新编辑文本,如下所示:

            //find your edit text
            EditText edit = (EditText) rowView.findViewById(R.id.mytext);
            //[A]display your text from yourArrayText base on position as usual
            edit.setText(yourArrayText.get(position).getText());
            //save the position for this edit text
            edit.setTag(position);

            edit.setOnFocusChangeListener(new OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus){
                        //get position from EditText which is been editing
                        int c_position = (Integer) v.getTag();
                        //update the text at the position, text will update at [A]
                        EditText input = (EditText)v;
                        yourArrayText.get(c_position).setText(input.getText().toString());
                    }
                }
            });