我正在基于REST API的响应在回收器视图中作为项目创建Editext。我需要从活动页面获取在列表中创建的EditText的值。在Adapter中创建EditText。
如何获取EditText值?
private void populateCheckBoxWithText(ViewHolder viewHolder, int position) {
int ctr = 1;
for (final String optionAns : arrayList.get(position).getOptionList()) {
Random rand = new Random();
CheckBox cBoxOption = new CheckBox(activity);
cBoxOption.setId(ctr);
cBoxOption.setTag(ctr);
cBoxOption.setText(optionAns);
editext_miltiple = new Custom_EditText_Regular(activity);
editext_miltiple.setId(ctr);
editext_miltiple.setTag(ctr);
editext_miltiple.setTextSize(12);
if (ctr != 1) {
cBoxOption.setLayoutParams(applyDesignParams(5, 1, 1, 1, 1));
editext_miltiple.setLayoutParams(applyDesignParams(5, 1, 1, 1, 1));
} else {
editext_miltiple.setLayoutParams(applyDesignParams(1, 1, 1, 1, 1));
}
editext_miltiple.setGravity(Gravity.CENTER_VERTICAL);
editext_miltiple.setBackground(null);
editext_miltiple.setHint("Enter response");
editext_miltiple.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
editext_miltiple.setPadding((int) AppData.convertDpToPixels(33, activity), (int) AppData.convertDpToPixels(5, activity), 0, 0);
viewHolder.sub_item.addView(cBoxOption);
viewHolder.sub_item.addView(editext_miltiple);
cBoxOption.setOnClickListener(getOnClickDoSomething(cBoxOption, arrayList.get(position).getQuestionID(), null, position, viewHolder));
//stringBuilder_multi.append(arrayList.get(position).getQuestionID() + ":" + editext_miltiple.getId() + ":");
//multi_select_txt=arrayList.get(position).getQuestionID() + ":" + editext_miltiple.getId() + ":";
editext_miltiple.addTextChangedListener(new GenericTextWatcher(editext_miltiple, position));
ctr++;
}
}
在上面的图像链接中,Editexts是循环动态创建的。我需要在活动中获取那些Editexts的值。
我需要创建这样的模式==> CheckBoxValue <> EditTextValue并将JSON发送到服务器。例如:技术<> edit_text_response,当用户单击复选框并在EditText中键入时。
感谢您的任何帮助!
您可以在类级别创建ArrayList来保存在循环中创建的EditText。创建EditText时,将它们添加到ArrayList中。喜欢
ArrayList<EditText> editTexts = new ArrayList<>();
然后在您的循环中添加
editTexts.add(editext_miltiple);
然后,您要访问EditList的任何地方都可以从ArrayList访问。