从循环中获取editext值

时间:2015-12-30 20:50:44

标签: android for-loop android-edittext edittextpreference

我被困在这里,我正在使用for循环来显示inflater布局多次充气布局包括编辑文本和spinner。这是我面临的主要问题是当我试图得到编辑文本中的整个文本,但只获取数组列表中的最后一个值编辑文本数据,但我需要All Above文本值

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

2 个答案:

答案 0 :(得分:0)

尝试从不同位置的编辑文本中获取数据,将其存储在列表中并使用。例如:

    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.
}

答案 1 :(得分:0)

您可以稍后View LinearLayout获取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();
        }
}});