从循环中获取编辑文本值
Get the editext values from the loop
我被困在这里,我使用 for 循环多次显示充气布局,充气布局由编辑文本组成,spinner.Here 是我面临的主要问题是,当我尝试获取编辑文本中的整个文本,但只获取数组列表中的最后一个值编辑文本数据,但我需要所有以上文本值
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>
尝试从不同位置的编辑文本中获取数据,将它们存储在列表中并使用。例如:
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.
}
稍后您可以通过 ll3.findViewWithTag("tag")
从 LinearLayout
获得 View
。
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();
}
}});
我被困在这里,我使用 for 循环多次显示充气布局,充气布局由编辑文本组成,spinner.Here 是我面临的主要问题是,当我尝试获取编辑文本中的整个文本,但只获取数组列表中的最后一个值编辑文本数据,但我需要所有以上文本值
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>
尝试从不同位置的编辑文本中获取数据,将它们存储在列表中并使用。例如:
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.
}
稍后您可以通过 ll3.findViewWithTag("tag")
从 LinearLayout
获得 View
。
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();
}
}});