验证recyler视图中的字段android

时间:2016-06-02 09:56:52

标签: java android android-layout

我需要验证recylerview中的每个字段。我没有找到执行验证的最佳方法。如果值为空,则每个Edittext字段都需要验证。在每个空值上显示toast消息

自定义XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/carview_margin">

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


            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                <EditText
                    android:id="@+id/ET_CT_foodeaten"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Food Eaten"
                    android:imeActionLabel="Food Eaten"
                    android:imeOptions="actionUnspecified"
                    android:maxLines="1"
                    android:singleLine="true" />
            </android.support.design.widget.TextInputLayout>


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <RelativeLayout
                    android:id="@+id/rel_serving"
                    android:layout_width="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:id="@+id/linear_ser"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_margin="@dimen/carview_margin"
                            android:text="Servings Eaten"
                            android:textSize="18dp" />

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginTop="-10dp"
                            android:background="@drawable/linear_layout_radius_custom"
                            android:orientation="horizontal">

                            <ImageView
                                android:layout_width="60sp"
                                android:id="@+id/minus_foodeaten"
                                android:layout_height="50sp"
                                android:padding="@dimen/carview_margin"
                                android:src="@drawable/minus" />

                            <View
                                android:layout_width="1dp"
                                android:layout_height="35sp"
                                android:layout_gravity="center"
                                android:background="@color/TabHeaderColor" />

                            <ImageView
                                android:layout_width="60sp"
                                android:layout_height="50sp"
                                android:id="@+id/add_foodeaten"
                                android:padding="@dimen/carview_margin"
                                android:src="@drawable/ic_add" />
                        </LinearLayout>


                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/linear_ser"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/IMG_info"
                            android:layout_width="wrap_content"
                            android:layout_marginRight="15dp"
                            android:layout_height="wrap_content"
                            android:src="@drawable/info" />

                        <TextView
                            android:id="@+id/value_serving"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginRight="15dp"
                            android:text="0"
                            android:textSize="18dp" />
                    </LinearLayout>


                </RelativeLayout>

                <View
                    android:layout_width="1dp"
                    android:id="@+id/space_view"
                    android:layout_height="80sp"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    android:background="@color/TabHeaderColor" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_toRightOf="@+id/rel_serving">

                    <TextView
                        android:id="@+id/label_food"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_gravity="center"
                        android:layout_margin="@dimen/carview_margin"
                        android:text="Food Group"
                        android:textSize="18dp" />

                    <Spinner
                        android:id="@+id/CS_food_group_spinner"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_below="@+id/label_food"
                        android:layout_margin="@dimen/carview_margin"
                        android:drawSelectorOnTop="true"
                        android:spinnerMode="dropdown" />
                </RelativeLayout>
            </RelativeLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

适配器代码

公共类CustomDataAdapterRecylerview扩展了RecyclerView.Adapter {     私人列表值;     私人活动活动;     AlertDialog alertDialogStores;     String [] foodLists;     FoodList [] val_list;     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;     FoodList food_list_value;

public CustomDataAdapterRecylerview(List<String> values, Activity context) {
    this.values = values;
    this.activity = context;
}

@Override
public CustomDataAdapterRecylerview.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.edittext_twospinner, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    final ViewHolder holder = viewHolder;
    final int pos = position;
    viewHolder.info.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPopUp();
        }
    });
    final ArrayAdapter<CharSequence> foodGroup_adapter = ArrayAdapter.createFromResource(activity, R.array.Food_Group, android.R.layout.simple_spinner_item);
    foodGroup_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    viewHolder.FoodGroup_spinner.setAdapter(foodGroup_adapter);
    foodLists = new String[100];
    val_list = new FoodList[100];
    food_list_value = new FoodList();


    viewHolder.FoodGroup_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            food_list_value.setServingsEaten(parent.getItemAtPosition(position).toString());

            SingletonAddValueFoodTracker.getInstance().setList_values(val_list);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


    viewHolder.setClickListener(new RecyclerViewItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
            if (view.getId() == holder.Add_Food_Item.getId()) {
                int val = Integer.parseInt(holder.valueserving.getText().toString());
                if (val < 15) {
                    val++;
                    food_list_value.setServingsEaten(String.valueOf(val));

                    SingletonAddValueFoodTracker.getInstance().setList_values(val_list);
                }
                holder.valueserving.setText(String.valueOf(val));
            } else if (view.getId() == holder.Minus_Food_Item.getId()) {
                int val = Integer.parseInt(holder.valueserving.getText().toString());
                if (val != 0) {
                    val--;
                    food_list_value.setServingsEaten(String.valueOf(val));

                    SingletonAddValueFoodTracker.getInstance().setList_values(val_list);
                }
                holder.valueserving.setText(String.valueOf(val));
            }
            {
                //Toast.makeText(view.getContext(), "ROW PRESSED = " + String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        }
    });


    viewHolder.Food_Eaten.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        @Override
        public void afterTextChanged(Editable s) {
                foodLists[pos] = String.valueOf(s);
                food_list_value.setFoodEaten(foodLists[pos]);
                val_list[pos] = food_list_value;
                SingletonAddValueFoodTracker.getInstance().setList_values(val_list);

        }
    });


}

@Override
public int getItemCount() {
    return values.size();
}

public void addItem(String country) {
    values.add(country);
    notifyItemInserted(values.size());
}

public void removeItem(int position) {
    values.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, values.size());
}

private void showPopUp() {

    // add your items, this can be done programatically
    // your items can be from a database
    List<FoodTrackerAlertDialogModel> ObjectItemData = new ArrayList<FoodTrackerAlertDialogModel>();
    ObjectItemData.add(new FoodTrackerAlertDialogModel("Fruit", "1 medium-sized piece of fruit", "1/2 cup berries", "3/4 cup fruit/veggie juice"));
    ObjectItemData.add(new FoodTrackerAlertDialogModel("Vege\ntables", "1/2 cup raw not-leafy veggies", "1 cup raw leafy veggies", "1 small baked potato"));
    ObjectItemData.add(new FoodTrackerAlertDialogModel("Dairy", "8 ounces milk", "1 cup yogurt", "1/2 ounces of cheese"));
    ObjectItemData.add(new FoodTrackerAlertDialogModel("Grains", "1 slice bread", "1 ounce dry cereal", "1/2 cup cooked rice or pasta"));
    ObjectItemData.add(new FoodTrackerAlertDialogModel("Lean\nMeat\nPoultry", "3 ounces cooked lean meat", "skinless poultry (about the size of a deck of cards)", "1 egg"));
    ObjectItemData.add(new FoodTrackerAlertDialogModel("Fish", "", "2 to 3 oz. (about the size of the palm of a woman&apos;s hand)", ""));
    ObjectItemData.add(new FoodTrackerAlertDialogModel("Snacks", "15 potato chips", "2 small cookies", "1/2 cup ice cream"));
    ObjectItemData.add(new FoodTrackerAlertDialogModel("Fats\nOils", "2 tablespoons light salad dressing", "1 tablespoon low-fat margarine or mayonnaise", "1 teaspoon vegetable oil"));

    // our adapter instance
    ListViewArrayAdapter adapter = new ListViewArrayAdapter(activity, R.layout.listview_fourtextview, ObjectItemData);

    // create a new ListView, set the adapter and item click listener
    ListView listViewItems = new ListView(activity);
    listViewItems.setAdapter(adapter);
    //listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem());

    // put the ListView in the pop up
    alertDialogStores = new AlertDialog.Builder(activity)
            .setView(listViewItems)
            .setTitle("Examples of what a serving\n").setPositiveButton("Back", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertDialogStores.dismiss();
                }
            })
            .show();

}


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
    EditText Food_Eaten;
    ImageView info, Add_Food_Item, Minus_Food_Item;
    Spinner FoodGroup_spinner;
    TextView valueserving;
    private RecyclerViewItemClickListener clickListener;

    public ViewHolder(View view) {
        super(view);
        info = (ImageView) view.findViewById(R.id.IMG_info);
        Food_Eaten = (EditText) view.findViewById(R.id.ET_CT_foodeaten);
        FoodGroup_spinner = (Spinner) view.findViewById(R.id.CS_food_group_spinner);
        valueserving = (TextView) view.findViewById(R.id.value_serving);
        Add_Food_Item = (ImageView) view.findViewById(R.id.add_foodeaten);
        Minus_Food_Item = (ImageView) view.findViewById(R.id.minus_foodeaten);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        Add_Food_Item.setOnClickListener(this);
        Minus_Food_Item.setOnClickListener(this);
    }

    public void setClickListener(RecyclerViewItemClickListener recyclerViewItemClickListener) {
        this.clickListener = recyclerViewItemClickListener;
    }

    @Override
    public void onClick(View v) {
        clickListener.onClick(v, getPosition(), false);
    }

    @Override
    public boolean onLongClick(View view) {
        clickListener.onClick(view, getPosition(), true);
        return true;
    }
}

}

enter image description here

如果编辑文本为空,请单击正确的图标,验证需要执行并显示祝酒消息。

1 个答案:

答案 0 :(得分:0)

您需要使用此检查并在else部分打印您的祝酒词以及if部分中的实施 -

if (edittext.getText().length() > 0) {
    // perform action here
} else {
    // show toast here if edittext is empty
}

希望它对你有所帮助。