Android 从循环中获取EditText值

Android 从循环中获取EditText值,android,for-loop,android-edittext,edittextpreference,Android,For Loop,Android Edittext,Edittextpreference,我被困在这里,我使用for循环多次显示充气机布局充气机布局充气布局由编辑文本和微调器组成。我面临的主要问题是,当我尝试在编辑文本中获取整个文本,但仅获取数组列表中最后一个值edit text data,但我需要全部以上文本值 Code.java LinearLayout ll3 = (LinearLayout) findViewById(R.id.ll3); View view; LayoutInflater layoutInflater = ge

我被困在这里,我使用for循环多次显示充气机布局充气机布局充气布局由编辑文本和微调器组成。我面临的主要问题是,当我尝试在编辑文本中获取整个文本,但仅获取数组列表中最后一个值edit text data,但我需要全部以上文本值

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);
        }
LinearLayout ll3=(LinearLayout)findViewById(R.id.ll3);
视图;
LayoutInflater LayoutInflater=getLayoutInflater();

对于(int i=1;i尝试从不同位置的编辑文本中获取数据,将它们存储在列表中并使用。例如:

    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.
}
List List=new ArrayList();
如果(i==0){
字符串数量=editText3.getText().toString();
list.add(i,数量);//或使用list.add(数量);
}否则{
//在充气机的其他位置继续执行相同操作。
}

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

    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.
}
List List=new ArrayList();
如果(i==0){
字符串数量=editText3.getText().toString();
list.add(i,数量);//或使用list.add(数量);
}否则{
//在充气机的其他位置继续执行相同操作。
}

您可以通过
ll3.findViewWithTag(“标记”)
稍后从
LinearLayout
获取
视图

LinearLayout ll3=(LinearLayout)findViewById(R.id.ll3);
视图;
LayoutInflater LayoutInflater=getLayoutInflater();

对于(int i=1;i您可以稍后通过
ll3从
LinearLayout
获取
视图

LinearLayout ll3=(LinearLayout)findViewById(R.id.ll3);
视图;
LayoutInflater LayoutInflater=getLayoutInflater();


对于(int i=1;我是指带有自定义适配器的listView?是的,为什么不使用
recyclerView
listView
?而不是对每个视图进行充气。但它必须与微调器链接,我希望微调器中显示字段的数量,先生,我只是循环充气器布局,然后将视图添加到该视图的LinearLayout set标记中然后稍后通过
ll3获取视图。findViewWithTag
尝试Sir不使用自定义适配器的Means listView?是的,为什么不使用
recyclerView
listView
?而不是膨胀每个视图。但它必须与微调器链接,我希望微调器Sir显示字段数,我只是循环inflLayout Layout在将视图添加到LinearLayout之前,将set标记添加到该视图中,然后通过
ll3获取视图。findViewWithTag
TroutedSir未工作列表必须为空,这就是为什么它会给出空指针错误…为方便起见进行了更新。还要确保检查您的代码。我已经这样声明了,但很抱歉,您遗漏了一些内容。它是有效的ks对我来说很好。列表永远不会给出空指针错误,应该用字符串更新。arraylist不是空的。现在它在toast Sir中显示空值。您的toast代码不可见。如果对您有帮助,请接受此答案。列表必须为空,这就是为什么它会给出空指针错误…为方便您而更新。还有m请务必检查您的代码。我只是这样声明的。很抱歉,您遗漏了一些东西。它对我来说很好。列表不应该出现空指针错误,应该用字符串更新。arraylist不是空的。现在它在toast Sir中显示空值。您的toast代码不可见。如果有帮助,请接受此答案给你。你不能在
视图
之前从
编辑文本
获取要呈现的值,所以将getter代码包含在
ll3.post(Runnable)
中,检查我的更新。你不能在
视图
之前从
编辑文本
获取要呈现的值,所以将getter代码包含在
ll3.post(Runnable)
中,检查我的更新。
    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();
        }
}});