1

I am stuck here at one point where I am using the for loop to display the inflater layout multiple time the inflated layout consists of edit text and the spinner.Here is the main problem I am facing is that when I try to get the entire text in the edit text but getting only the last value edit text data in the array list but I need the All Above text values

Code.java

        LinearLayout ll3 = (LinearLayout) findViewById(R.id.ll3);
        View view;
        LayoutInflater layoutInflater = getLayoutInflater();
        for (int i = 1; i <= 4; i++) {
            view = layoutInflater.inflate(R.layout.specify_custom_layout, ll3, false);
            constantname = (TextView) view.findViewById(R.id.constantname);
            constantname.setText("Medicine" + i);
            selectionname = (MaterialSpinner) view.findViewById(R.id.selectionname);
            ArrayAdapter<String> slectionadapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.specify_text, select);
            slectionadapter.setDropDownViewResource(R.layout.spinner_dropdown);
            selectionname.setAdapter(slectionadapter);
            editText3 = (EditText) view.findViewById(R.id.editText3);
            String quantity = editText3.getText().toString();
            List<String> data = new ArrayList<String>();
                data.add(quantity);
            ll3.addView(view);
        }

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ll">

    <TextView
        android:id="@+id/constantname"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="Medicine"
        android:textSize="20dp"
        android:textColor="#000"
        android:textColorHint="#000" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/constantname"
        android:textColor="#000"
        android:inputType="number"
        android:textColorHint="#000" />

    <fr.ganfra.materialspinner.MaterialSpinner
        android:id="@+id/selectionname"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        app:ms_arrowSize="16dp"
        android:layout_toRightOf="@+id/editText3"
        app:ms_baseColor="@color/colorPrimary"
        app:ms_enableFloatingLabel="false"
        app:ms_hint="Med Type"
        app:ms_thickness="1dp"
        />

</RelativeLayout>

This is the ScreenShot

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
prominere
  • 79
  • 2
  • 10

2 Answers2

0

Try getting data from edit text at different positions, store them in the list and use. For example:

    List<String> list = new ArrayList<>();
if (i == 0) {
    String quantity = editText3.getText().toString();
    list.add(i, quantity); //or use list.add(quantity);
} else {
    //continue doing the same at other postions of inflater.
}
Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
0

You can get View later from LinearLayout by ll3.findViewWithTag("tag").

    LinearLayout ll3 = (LinearLayout) findViewById(R.id.ll3);
    View view;
    LayoutInflater layoutInflater = getLayoutInflater();
    for (int i = 1; i <= 4; i++) {
        view = layoutInflater.inflate(R.layout.specify_custom_layout, ll3, false);
        constantname = (TextView) view.findViewById(R.id.constantname);
        constantname.setText("Medicine" + i);
        selectionname = (MaterialSpinner) view.findViewById(R.id.selectionname);
        ArrayAdapter<String> slectionadapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.specify_text, select);
        slectionadapter.setDropDownViewResource(R.layout.spinner_dropdown);
        selectionname.setAdapter(slectionadapter);
        editText3 = (EditText) view.findViewById(R.id.editText3);
        String quantity = editText3.getText().toString();
        List<String> data = new ArrayList<String>();
            data.add(quantity);
        view.setTag("myView"+i); // add tag to view to be returned later from ll3
        ll3.addView(view);
    }


ll3.post (new Runnable(){
        public void run (){
        for (int i = 1; i <= 4; i++) {
        final View v = ll3.findViewWithTag("myView"+i);
        EditText et = (EditText) v.findViewById(R.id.editText3);
        String etText = et.getText().toString();
        }
}});
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
  • You can't obtain value from `EditText` before `View` to be rendered so enclose getter code inside `ll3.post (Runnable)`, Check my update. – Khaled Lela Jan 01 '16 at 06:08