来自recyleview适配器的动态Edittext字段中的验证

时间:2018-11-26 09:03:27

标签: android

我已经使用recylerview视图适配器创建了动态edittext表单。我需要验证所有空字段。

  1. 我在VendorFormActivity上具有“提交”按钮,并且必须从适配器的字段进行提交,因此我已经从适配器创建了单击活动的按钮。

  2. 我在提交按钮时也从适配器调用了改造API。

  3. 我有四种情况的JSON数据类型:字符串,整数,日期和下拉列表类型,以在Android中创建表单。

  4. 问题出在某些字段中,只有字符串类型,如果我一次验证所有内容,应用程序将崩溃,并且仅在最后一个字段中显示验证消息。因此,遇到了问题。

  5. 我还需要询问我需要调用哪种方法来调用适配器上的Submit按钮,或者需要从Activity本身进行调用。现在,已经在onBindViewHolder上调用了Submit按钮。

  6. 适配器代码发布在这里:

@Override
public void onBindViewHolder(final DynamicFormAdapter.MyViewHolder holder, final int position) {


    final HelpForm.FormSetting formSetting = formArrayList.get(position);


    if (formSetting.getType().equals("STRING")) {


        holder.textViewString = new TextView(activity);
        holder.linearLayout.addView(holder.textViewString);
        holder.textViewString.setText(formSetting.getLabel());
        holder.textViewString.setPadding(24, 4, 24, 2);
        holder.textViewString.setTextColor(context.getResources().getColor(R.color.colorTexts));
        holder.textViewString.setTextSize(15);


        holder.autoCompleteFields = new AutoCompleteTextView(activity);
        holder.linearLayout.addView(holder.autoCompleteFields);
        holder.autoCompleteFields.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) {

                formSetting.setAnswer(s.toString());
                Log.e("VALUEstringsssss ", formSetting.getAnswer());


            }
        });
        holder.textViewString.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> gee = new ArrayList<>();
                gee.add("hello");
                gee.add("namaste");
                buttonCallBacks.onButtonClick(gee);
            }
        });


    }

    if (formSetting.getType().equals("INT")) {
        Log.d("cjjdnjcnjd", formSetting.getId());

        holder.autoCompleteFields = new AutoCompleteTextView(activity);


        holder.textViewInt = new TextView(activity);
        holder.linearLayout.addView(holder.textViewInt);
        holder.textViewInt.setText(formSetting.getLabel());
        holder.textViewInt.setPadding(24, 4, 24, 2);
        holder.textViewInt.setTextColor(context.getResources().getColor(R.color.colorTexts));
        holder.textViewInt.setTextSize(15);


        holder.linearLayout.addView(holder.autoCompleteFields);
        holder.autoCompleteFields.setInputType(InputType.TYPE_CLASS_NUMBER);


        holder.autoCompleteFields.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) {
                formSetting.setAnswer(s.toString());
                IntValue = s.toString();
                editTextInt.add(formSetting.getAnswer());


            }
        });

    }
    if (formSetting.getType().equals("DATE")) {
        holder.textViewDate = new TextView(context);
        holder.editTextDate = new EditText(context);
        holder.autoCompleteFields = new AutoCompleteTextView(context);
        holder.linearLayout.addView(holder.textViewDate);
        holder.linearLayout.addView(holder.editTextDate);
        holder.linearLayout.addView(holder.autoCompleteFields);

        holder.textViewDate.setText(formSetting.getLabel());
        holder.textViewDate.setPadding(24, 4, 24, 2);
        holder.textViewDate.setTextColor(context.getResources().getColor(R.color.colorTexts));
        holder.textViewDate.setTextSize(15);

        holder.editTextDate.setCursorVisible(false);
        holder.editTextDate.setFocusable(false);
        holder.autoCompleteFields.setVisibility(View.GONE);

        holder.editTextDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.showSoftInput(holder.editTextDate, InputMethodManager.SHOW_FORCED);
                final Calendar c = Calendar.getInstance();
                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog datePickerDialog = new DatePickerDialog(context,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(android.widget.DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {


                                // holder.editTextDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                                SimpleDateFormat fr = new SimpleDateFormat("dd-MM-yyyy");
                                mydate = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
                                //  textViewShow.setText(dd);
                                Date date = null;
                                try {
                                    date = fr.parse(mydate);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                long milliseconds = date.getTime();
                                Log.d("cbhfeey", String.valueOf(milliseconds));
                                holder.autoCompleteFields.setText(String.valueOf(milliseconds));
                                holder.editTextDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                            }
                        }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });


        holder.autoCompleteFields.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) {


                formSetting.setAnswer(s.toString());

            }
        });
    }


    if (formSetting.getType().equals("DROPDOWN")) {
        holder.textViewSpinner = new TextView(context);
        holder.linearLayout.addView(holder.textViewSpinner);
        holder.textViewSpinner.setText(formSetting.getLabel());
        holder.textViewSpinner.setPadding(16, 4, 24, 4);

        holder.autoCompleteFields = new AutoCompleteTextView(context);

        holder.autoCompleteFields.setCursorVisible(false);
        holder.autoCompleteFields.setFocusable(false);
        holder.linearLayout.addView(holder.autoCompleteFields);
        holder.autoCompleteFields.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.dropdown, 0);
        //autoCompleteTextViewSpinner.setPadding(0,0,16,0);

        ArrayList<String> err = new ArrayList<>();
        err = formSetting.getOptions();

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                context, android.R.layout.simple_list_item_1, err);
        holder.autoCompleteFields.setAdapter(arrayAdapter);
        holder.autoCompleteFields.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.autoCompleteFields.showDropDown();
            }
        });

        holder.autoCompleteFields.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) {
                formSetting.setAnswer(s.toString());


            }
        });




    }
    VendorFormActivity.save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            JsonArray datas = new JsonArray();

            for (HelpForm.FormSetting pn : formArrayList) {

                JsonObject object = new JsonObject();
                object.addProperty("id", pn.getId());
                object.addProperty("label", pn.getLabel());
                object.addProperty("value", pn.getAnswer());
                datas.add(object);


            }

            JsonObject req = new JsonObject();
            req.addProperty("jjj", new Gson().toJson(datas));
            ArrayList<String> arrayList = new ArrayList<>();
            arrayList.add(String.valueOf(datas));

            Log.d("hellodatas", String.valueOf(datas));

            if (holder.autoCompleteFields.getText().toString().isEmpty()){
                holder.autoCompleteFields.setError("Field must not be empty!");
            }else {
                callRetrofitSaveForm(datas);

            }


        }
    });


}



public void callRetrofitSaveForm(JsonArray data) {


    userID = new PrefsManager(MasterApplication.getmInstance()).getUserId();

    vendorID = new PrefsManager(MasterApplication.getmInstance()).getVendorId();
    Log.d("jjdjd", vendorID);
    RestClient.RetroInterfaceAPI mInterface = RestClient.getClient();
    Call<SaveForm> call = mInterface.getSaveForm(vendorID, userID, String.valueOf(data));
    call.enqueue(new Callback<SaveForm>() {
        @Override
        public void onResponse(Call<SaveForm> call, Response<SaveForm> response) {
            Log.d("jcjdcjj", "" + response.code());
            if (response.code() == 500) {
                Toast.makeText(context, context.getResources().getText(R.string.server_error), Toast.LENGTH_SHORT).show();

            }

            if (response.body() != null) {

                if (response.body().getMessage().equals("success")) {
                    Toast.makeText(context, context.getResources().getText(R.string.success_form), Toast.LENGTH_SHORT).show();
                    //((VendorFormActivity)context).recreate();
                    ((VendorFormActivity) context).onBackPressed();
                }


            } else {

            }


        }

        @Override
        public void onFailure(Call<SaveForm> call, Throwable t) {

        }
    });
}


@Override
public int getItemCount() {

    return formArrayList.size();

}

@Override
public int getItemViewType(int position) {
    return position;
}


public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView textViewString, textViewInt, textViewDate;
    ViewGroup linearLayout;
    EditText editTextINT, editTextDate, hideEditTextDate;
    Spinner spinner;
    TextView textViewSpinner;
    AutoCompleteTextView autoCompleteFields;


    public MyViewHolder(View itemView) {
        super(itemView);
        linearLayout = itemView.findViewById(R.id.dynamiccontainer);


    }


}

0 个答案:

没有答案