How to get dynamic created EditText Data in fragment?

44 views Asked by At

I am working on android application in which I am creating dynamic Layout on fragment. When user click on button to get data from layout element then I am getting null pointer exception error.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_schedule, container, false);

        parentLinearLayout=(LinearLayout) view.findViewById(R.id.parent_linear_layout);
        add_field_button=(TextView) view.findViewById(R.id.add_field_button);
        txtDate=(TextView) view.findViewById(R.id.txtDate);
        txtTime=(TextView) view.findViewById(R.id.txtTime);
        btnSubmit=(Button) view.findViewById(R.id.btnSubmit);

        buttomclick(view);


        return view;


    }

I am calling below function to get data from dynamic layout and I am getting null pointer exception on EditText.

    ArrayList getEditTextList()
    {

        ArrayList<String> list = new ArrayList<String>();
        int size = parentLinearLayout.getChildCount();

        for (int i=0 ; i < size ; i++)
        {
            View view = parentLinearLayout.getChildAt(i);
            EditText edtUsername = view.findViewById(R.id.edtVisitorname);
            list.add(edtUsername.getText().toString());
            String a ="";
        }
        return list;
    }

On click on below button I am adding Dynamic layout.

add_field_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAddField(v);
            }
        });


public void onAddField(View v) {
        LayoutInflater inflater=(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView=inflater.inflate(R.layout.field, null);
        // Add the new row before the add field button.
        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
    }
0

There are 0 answers