if 语句不检查通过滚动 recyclerview 到达的 edittexts 的值

if statement doesn't check the values of the edittexts reached by scrolling the recyclerview

我有一个回收视图,每行都有一个编辑文本。用户可以根据需要更改 edittext 的值,但不应将其留空,因为这些值将保存在数据库中。为此,我正在尝试检查 edittext 是否为空。如果任何行的编辑文本为空,则当用户想要保存时会收到一条消息,提示该行存在空值。这是我写的代码:

  public class recyclerview_viewholder : RecyclerView.ViewHolder
        {
            public TextView rownbr, itemname;
            public EditText qty;
            public TextView unit;
            public LinearLayout linearLayout;
           
            public recyclerview_viewholder(View itemView, Action<int> listener)
              : base(itemView)
            {
                rownbr = itemView.FindViewById<TextView>(Resource.Id.rownbr);
                itemname = itemView.FindViewById<TextView>(Resource.Id.laborname);
                unit = itemView.FindViewById<TextView>(Resource.Id.days);
                qty = itemView.FindViewById<EditText>(Resource.Id.overtime);
                linearLayout = itemView.FindViewById<LinearLayout>(Resource.Id.linearLayout);
               
                itemView.Click += (sender, e) => listener(base.LayoutPosition);
               
            }
        }

        public class recyclerviewAdapter : RecyclerView.Adapter
        {
            // Event handler for item clicks:
            public event EventHandler<int> ItemClick;

            DataTable summary_Requests = new DataTable();
            //Context context;
            public readonly new_request_items context;
            int selected_pos = -1;
            private SwipeToDeleteCallback swipeToDeleteCallback;
            List<list_item> item_details = new List<list_item>();
            public recyclerviewAdapter(new_request_items context, DataTable sum_req, List<list_item> item_details)
            {
                this.context = context;
                summary_Requests = sum_req;
                this.item_details = item_details;

            }
            public recyclerviewAdapter(DataTable sum_req, SwipeToDeleteCallback swipeToDeleteCallback)
            {
                this.swipeToDeleteCallback = swipeToDeleteCallback;
                summary_Requests = sum_req;

            }
            public override RecyclerView.ViewHolder
                   OnCreateViewHolder(ViewGroup parent, int viewType)
            {
                View itemView = LayoutInflater.From(parent.Context).
                            Inflate(Resource.Layout.recycler_view_request_new_data, parent, false);


                recyclerview_viewholder vh = new recyclerview_viewholder(itemView, OnClick);

                vh.qty.TextChanged += (sender, e) =>
                {
                    if (vh.qty.Text != "")

                        try
                        {
                            int position = vh.LayoutPosition;
                            summary_Requests.Rows[position]["itemQty"] = Convert.ToDecimal(vh.qty.Text);
                            user.zero_val = "Not_exist";
                        }
                        catch (System.FormatException exp)
                        {
                            var icon = AppCompatResources.GetDrawable(context.Context, Resource.Drawable.error_ic);
                            icon.SetBounds(0, 0, 50, 50);
                            vh.qty.SetError("qty can be decimal", icon);
                            user.zero_val = "exits";
                        }
                    else if (vh.qty.Text == "")
                    {
                        var icon = AppCompatResources.GetDrawable(context.Context, Resource.Drawable.error_ic);
                        icon.SetBounds(0, 0, 50, 50);
                        vh.qty.SetError("value can not be empty", icon);
                        user.zero_val = "exits";
                    }

                };
                vh.ItemView.LongClick += (sender, e) =>
                {
                    int position = vh.AdapterPosition;
                    string itemcode = summary_Requests.Rows[position]["itemcode"].ToString();


                    list_item result = item_details.Find(list_item => list_item.item_code == itemcode);
                    Bundle bundle = new Bundle();
                    bundle.PutString("result", JsonConvert.SerializeObject(result));
                    items_info iteminf = new items_info();
                    iteminf.Arguments = bundle;
                    iteminf.Cancelable = true;
                    var SupportFragmentManager = this.context.FragmentManager;
                    iteminf.Show(SupportFragmentManager, "dialog");
               
                    selected_pos = position;
                    NotifyDataSetChanged();
                    //fill global variables that need to be passed to detail fragment



                };

                return vh;
            }
            public override void
                  OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
            {
                recyclerview_viewholder vh = holder as recyclerview_viewholder;

              
                vh.rownbr.Text = summary_Requests.Rows[position]["rowNumber"].ToString();
                vh.itemname.Text = summary_Requests.Rows[position]["name"].ToString();
                vh.unit.Text = summary_Requests.Rows[position]["itemsunitcode"].ToString();
                vh.qty.Text= summary_Requests.Rows[position]["itemQty"].ToString();
                if (selected_pos == position)
                    vh.ItemView.SetBackgroundColor(Color.ParseColor("#4fa5d5"));
                else
                    vh.ItemView.SetBackgroundColor(Color.LightGray);

               
            }



            public void RemoveItem(int position)
            {
                
                if (laborers_dt_total.Rows.Count != 0)
                {
                    if (position < laborers_dt_total.Rows.Count && position > -1)
                    {
                        laborers_dt_total.Rows.RemoveAt(position);
                     
                    }
                    else
                    {
                        Toast.MakeText(context.Context, "select an item to delete", ToastLength.Long).Show();

                    }

                }
                else if (laborers_dt_total.Rows.Count == 0)
                {
                    Toast.MakeText(context.Context, "no items to delete", ToastLength.Long).Show();

                }


                for (int i = 0; i < laborers_dt_total.Rows.Count; i++)
                {
                    laborers_dt_total.Rows[i]["rowNumber"] = (i + 1).ToString();
                   
                    NotifyDataSetChanged();
                    
                }

            }
            public DataTable get_dt_final()
            {
                DataTable final_dt = summary_Requests.Copy();
                return final_dt;
            }
            public override int ItemCount
            {
                get { return summary_Requests.Rows.Count; }
            }
            // Raise an event when the item-click takes place:
            void OnClick(int position)
            {
                if (ItemClick != null)
                    ItemClick(this, position);
                // user.req_pos = position;


            }
         

        }

现在当 recyclerview 中只有几行时它工作得很好,但是当我必须在其中滚动很多行时,如果我在滚动到达的一行中保持一个 edittext 为空,user.zero_val 没有使用 exits 的值来告诉我编辑文本是空的。在这种情况下我该怎么办?我在哪里检查空的编辑文本?提前致谢。

这是我编写的一个简单代码,希望它能说明我的问题:

这是 recyclerview 的 axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.RecyclerView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="255sp"
        android:id="@+id/recyclerView1" />
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1" />


</LinearLayout>

这是recyclerview每一行的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"                    
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/light_grey"
        android:padding="1dp"
        android:layout_marginTop="0.5dp"
        android:weightSum="8"
        android:gravity="center"
        android:id="@+id/linearLayout"
    
                >
    
    <TextView
        android:text=""
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:id="@+id/rownbr" 
        android:background="@drawable/back"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:textSize="12dp"
        android:gravity="center"
        android:textColor="#000000"
    />
    <TextView
        android:text=""
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="4"
        android:id="@+id/laborname" 
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:textSize="12dp"
        android:gravity="center"
        android:textColor="#000000"
        android:background="@drawable/back"
        android:layout_marginLeft="2dp"/>
    <TextView
        android:text=""
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:id="@+id/days" 
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:textSize="12dp"
        android:textColor="#000000"
        android:layout_marginLeft="2dp"
        android:gravity="center"
        android:background="@drawable/back"/>
    
    <EditText
        android:text=""
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:id="@+id/overtime" 
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:textSize="12dp"
        android:textColor="#000000"
        android:layout_marginLeft="2dp"
        android:gravity="center"
        android:background="@drawable/back"/>
        
</LinearLayout>

这是 back.xml 可绘制对象:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
  <solid android:color="@android:color/white" />
  <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

这是填充和管理 recyclerview 的 activity:

 public class MainActivity : AppCompatActivity
    {
        RecyclerView mRecyclerView;
        DataTable dt = new DataTable();
       
        RecyclerView.LayoutManager mLayoutManager;
        recyclerviewAdapter RecyclerviewAdapter;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            dt.Columns.Add("rowNumber");
            dt.Columns.Add("name");
            dt.Columns.Add("itemsunitcode");
            dt.Columns.Add("itemQty");

            dt.Rows.Add("1", "rana hd", "pcs", "1");
            dt.Rows.Add("2", "rana hd1", "pcs", "1");
            dt.Rows.Add("3", "rana hd2", "pcs", "1");
            dt.Rows.Add("4", "rana hd3", "pcs", "1");
            dt.Rows.Add("5", "rana hd4", "pcs", "1");
            dt.Rows.Add("6", "rana hd5", "pcs", "1");
            dt.Rows.Add("7", "rana hd6", "pcs", "1");
            dt.Rows.Add("8", "rana hd7", "pcs", "1");
            dt.Rows.Add("9", "rana hd8", "pcs", "1");
            dt.Rows.Add("10", "rana hd9", "pcs", "1");
          
          
            mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView1);
            Button btn= FindViewById<Button>(Resource.Id.button1);
            mLayoutManager = new LinearLayoutManager(this);
            mRecyclerView.HasFixedSize = true;
            mRecyclerView.SetLayoutManager(mLayoutManager);
            RecyclerviewAdapter = new recyclerviewAdapter(this, dt);
            mRecyclerView.SetAdapter(RecyclerviewAdapter);

            btn.Click += delegate
            {
                if (user.zero_val == "exits")
                    Toast.MakeText(this, "exists", ToastLength.Long).Show();
                if (user.zero_val == "Not_exist")
                    Toast.MakeText(this, "Not_exist", ToastLength.Long).Show();
            };
        }
        public class recyclerview_viewholder : RecyclerView.ViewHolder
        {
            public TextView rownbr, itemname;
            public EditText qty;
            public TextView unit;
            public LinearLayout linearLayout;

            public recyclerview_viewholder(View itemView, Action<int> listener)
              : base(itemView)
            {
                rownbr = itemView.FindViewById<TextView>(Resource.Id.rownbr);
                itemname = itemView.FindViewById<TextView>(Resource.Id.laborname);
                unit = itemView.FindViewById<TextView>(Resource.Id.days);
                qty = itemView.FindViewById<EditText>(Resource.Id.overtime);
                linearLayout = itemView.FindViewById<LinearLayout>(Resource.Id.linearLayout);

                itemView.Click += (sender, e) => listener(base.LayoutPosition);

            }
        }

        public class recyclerviewAdapter : RecyclerView.Adapter
        {
            // Event handler for item clicks:
            public event EventHandler<int> ItemClick;

            DataTable summary_Requests = new DataTable();
            //Context context;
            public readonly MainActivity context;
            int selected_pos = -1;
             public recyclerviewAdapter(MainActivity context, DataTable sum_req)
            {
                this.context = context;
                summary_Requests = sum_req;
             
            }
          
            public override RecyclerView.ViewHolder
                   OnCreateViewHolder(ViewGroup parent, int viewType)
            {
                View itemView = LayoutInflater.From(parent.Context).
                            Inflate(Resource.Layout.recyclerview_data, parent, false);


                recyclerview_viewholder vh = new recyclerview_viewholder(itemView, OnClick);

                vh.qty.TextChanged += (sender, e) =>
                {
                    if (vh.qty.Text != "")

                        try
                        {
                            int position = vh.LayoutPosition;
                            summary_Requests.Rows[position]["itemQty"] = Convert.ToDecimal(vh.qty.Text);
                            user.zero_val = "Not_exist";
                        }
                        catch (System.FormatException exp)
                        {
                            var icon = AppCompatResources.GetDrawable(context, Resource.Drawable.error_ic);
                            icon.SetBounds(0, 0, 50, 50);
                            vh.qty.SetError("qty can be decimal", icon);
                            user.zero_val = "exits";
                        }
                    else if (vh.qty.Text == "")
                    {
                        var icon = AppCompatResources.GetDrawable(context, Resource.Drawable.error_ic);
                        icon.SetBounds(0, 0, 50, 50);
                        vh.qty.SetError("value can not be empty", icon);
                        user.zero_val = "exits";
                    }

                };
                vh.ItemView.LongClick += (sender, e) =>
                {
                    int position = vh.AdapterPosition;
                  
                    selected_pos = position;
                    NotifyDataSetChanged();
                
                };

                return vh;
            }
            public override void
                  OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
            {
                recyclerview_viewholder vh = holder as recyclerview_viewholder;


                vh.rownbr.Text = summary_Requests.Rows[position]["rowNumber"].ToString();
                vh.itemname.Text = summary_Requests.Rows[position]["name"].ToString();
                vh.unit.Text = summary_Requests.Rows[position]["itemsunitcode"].ToString();
                vh.qty.Text = summary_Requests.Rows[position]["itemQty"].ToString();
                if (selected_pos == position)
                    vh.ItemView.SetBackgroundColor(Color.ParseColor("#4fa5d5"));
                else
                    vh.ItemView.SetBackgroundColor(Color.LightGray);
            }
         
            public DataTable get_dt_final()
            {
                DataTable final_dt = summary_Requests.Copy();
                return final_dt;
            }
            public override int ItemCount
            {
                get { return summary_Requests.Rows.Count; }
            }
            // Raise an event when the item-click takes place:
            void OnClick(int position)
            {
                if (ItemClick != null)
                    ItemClick(this, position);
                // user.req_pos = position;


            }
        }
    }
}

这是用户 class:

 public static class user
    {
        public static string zero_val = "";
    }

如果我没有正确理解你的意思,我认为在 class user 中使用变量 zero_val 来指示是否为空值是不正确的 exist.For例如,如果您为第一个 EditText 输入了一个空值,那么 zero_val 的值将更改为 Not_exist,那么如果我们为第二个 EditText 输入另一个空值,则 zero_val也会被改成Not_exist,之后如果我们把第二个EditText改成正确的值,那么zero_val的值就会改成exits。但是现在, 其实第一个EditText还是空的

我认为您可以在单击按钮时验证 table DataTable 的值。

您可以参考以下代码:

        btn.Click += delegate
        {
            bool hasEmpty = false ;
            foreach (DataRow row in dt.Rows)
            {
                string itemQty = row["itemQty"].ToString();
                string rowNumber = row["rowNumber"].ToString();

                System.Diagnostics.Debug.WriteLine("rowNumber =" + rowNumber  + "< ---  > itemQty = " + itemQty);
                if (string.IsNullOrEmpty(itemQty)) {
                    hasEmpty = true;
                }
            }

            //if (user.zero_val.Equals("exits"))
            //    Toast.MakeText(this, "exists", ToastLength.Long).Show();
            //if (user.zero_val.Equals("Not_exist"))
            //    Toast.MakeText(this, "Not_exist", ToastLength.Long).Show();

            if (!hasEmpty)
                Toast.MakeText(this, "exists", ToastLength.Long).Show();
            else
                Toast.MakeText(this, "Not_exist", ToastLength.Long).Show();
        };

另外,修改RecyclerviewAdapter中的TextChanged函数:

        vh.qty.TextChanged += (sender, e) =>
        {
            if (!vh.qty.Text.Equals(""))

                try
                {
                    int position = vh.LayoutPosition;
                    summary_Requests.Rows[position]["itemQty"] = Convert.ToDecimal(vh.qty.Text);
                }
                catch (System.FormatException exp)
                {
                    var icon = AppCompatResources.GetDrawable(context, Resource.Drawable.error);
                    icon.SetBounds(0, 0, 50, 50);
                    vh.qty.SetError("qty can be decimal", icon);

                    //store a empty value for this textview
                    int position = vh.LayoutPosition;
                    summary_Requests.Rows[position]["itemQty"] = "";
                }
            else if (vh.qty.Text.Equals(""))
            {
                var icon = AppCompatResources.GetDrawable(context, Resource.Drawable.error);
                int position = vh.LayoutPosition;

                //store a empty value for this textview
                summary_Requests.Rows[position]["itemQty"] = "";

                icon.SetBounds(0, 0, 50, 50);
                vh.qty.SetError("value can not be empty", icon);

            }
        };