在 Recyclerview 中设置 edittext
Setting edittext inside Recyclerveiw
我有一个包含 edittext 和 textview 的 recyclerview,我在 recyclerview 外面有一个按钮。我想在单击按钮 (edittext.setError("required")) 时验证 edittext,为此我使用了 addTextChangedListener 并在 onTextChanged() 方法中将 charSequence 存储在 Hashmap 中。下面是我的 onBindViewHolder
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
String label = mData.get(position).getLabel();
holder.label.setText(label);
holder.label_input.setTag(position);
holder.label_input.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// Log.w("ontextchanged","i = "+i+" i1 = "+i1+" i2 = "+i2+""+"char seq "+charSequence.toString()+"position = "+position);
stringHashMap.put(""+position,""+charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
并在 activity 中单击按钮时根据位置获取存储的值,下面是 Activity 按钮 onclick
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i< TimeSheetDetailAdapter.stringHashMap.size(); i++) {
//Entered edittext value from hashmap
String data = TimeSheetDetailAdapter.stringHashMap.get(""+i);
}
}
});
我正在根据位置正确获取数据中输入的 edittext 值,但问题是我想验证每个 edittext,如果数据为空,我想用 edittext.setError 设置特定的 edittext ("required") ,为此我已经尝试了
if(recyclerView.findViewHolderForLayoutPosition(i) instanceof TimeSheetDetailAdapter.ViewHolder){
TimeSheetDetailAdapter.ViewHolder childHolder = (TimeSheetDetailAdapter.ViewHolder) recyclerView.findViewHolderForLayoutPosition(i);
childHolder.label_input.setError("required");
但是它设置错误为仅在屏幕上可见的项目,即只有当编辑文本可见时它才会设置,不可见的项目它不会设置。我知道 recyclerview 的概念,我也尝试过使用 findViewHolderForAdapterPosition(),但没有帮助,请帮我解决这个问题,我如何访问不可见的视图并设置 eroor 或任何其他方式。
提前致谢
您可以制作一个数组列表,其中包含要在回收站列表中显示的消息,分别按其位置排列。
并访问该数组列表以将其设置在标签中。我认为这可能是唯一的解决方案。因为您无权访问不可见的列表项。
我最终找到了更好的解决方案
if (data.equals("empty") || data.equals("")) {
isAllDataFilled = false;
//Scrolling to the respective position
linearLayoutManager.scrollToPositionWithOffset(i, 0);
//giving delay of 200ms to get the view and then setting error for that
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
TimeSheetDetailAdapter.ViewHolder childHolder = (TimeSheetDetailAdapter.ViewHolder) recyclerView.findViewHolderForLayoutPosition(i);
childHolder.label_input.setError("error");
}
}, 200);
enteredDataList.clear();
break;
}
谢谢
我有一个包含 edittext 和 textview 的 recyclerview,我在 recyclerview 外面有一个按钮。我想在单击按钮 (edittext.setError("required")) 时验证 edittext,为此我使用了 addTextChangedListener 并在 onTextChanged() 方法中将 charSequence 存储在 Hashmap 中。下面是我的 onBindViewHolder
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
String label = mData.get(position).getLabel();
holder.label.setText(label);
holder.label_input.setTag(position);
holder.label_input.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// Log.w("ontextchanged","i = "+i+" i1 = "+i1+" i2 = "+i2+""+"char seq "+charSequence.toString()+"position = "+position);
stringHashMap.put(""+position,""+charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
并在 activity 中单击按钮时根据位置获取存储的值,下面是 Activity 按钮 onclick
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i< TimeSheetDetailAdapter.stringHashMap.size(); i++) {
//Entered edittext value from hashmap
String data = TimeSheetDetailAdapter.stringHashMap.get(""+i);
}
}
});
我正在根据位置正确获取数据中输入的 edittext 值,但问题是我想验证每个 edittext,如果数据为空,我想用 edittext.setError 设置特定的 edittext ("required") ,为此我已经尝试了
if(recyclerView.findViewHolderForLayoutPosition(i) instanceof TimeSheetDetailAdapter.ViewHolder){
TimeSheetDetailAdapter.ViewHolder childHolder = (TimeSheetDetailAdapter.ViewHolder) recyclerView.findViewHolderForLayoutPosition(i);
childHolder.label_input.setError("required");
但是它设置错误为仅在屏幕上可见的项目,即只有当编辑文本可见时它才会设置,不可见的项目它不会设置。我知道 recyclerview 的概念,我也尝试过使用 findViewHolderForAdapterPosition(),但没有帮助,请帮我解决这个问题,我如何访问不可见的视图并设置 eroor 或任何其他方式。
提前致谢
您可以制作一个数组列表,其中包含要在回收站列表中显示的消息,分别按其位置排列。
并访问该数组列表以将其设置在标签中。我认为这可能是唯一的解决方案。因为您无权访问不可见的列表项。
我最终找到了更好的解决方案
if (data.equals("empty") || data.equals("")) {
isAllDataFilled = false;
//Scrolling to the respective position
linearLayoutManager.scrollToPositionWithOffset(i, 0);
//giving delay of 200ms to get the view and then setting error for that
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
TimeSheetDetailAdapter.ViewHolder childHolder = (TimeSheetDetailAdapter.ViewHolder) recyclerView.findViewHolderForLayoutPosition(i);
childHolder.label_input.setError("error");
}
}, 200);
enteredDataList.clear();
break;
}
谢谢