试试这个
添加更多适配器
public class AddMoreAdapter extends RecyclerView.Adapter<AddMoreAdapter.ViewHolder> {
Context context;
ArrayList<AddMorePojo> arrayList;
public AddMoreAdapter(Context context, ArrayList<AddMorePojo> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public AddMoreAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.addmorelayout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(AddMoreAdapter.ViewHolder holder, int position) {
Log.e("VALUE ", arrayList.get(position).getUserName());
if (!TextUtils.isEmpty(arrayList.get(position).getUserName())) {
holder.edtName.setText(arrayList.get(position).getUserName());
}
if (!TextUtils.isEmpty(arrayList.get(position).getError())) {
holder.edtName.setError(arrayList.get(position).getError());
} else {
holder.edtName.setError(null);
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
public ArrayList<AddMorePojo> getArrayList() {
return arrayList;
}
public class ViewHolder extends RecyclerView.ViewHolder {
EditText edtName;
public ViewHolder(View itemView) {
super(itemView);
edtName = itemView.findViewById(R.id.edtUname);
edtName.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) {
arrayList.get(getAdapterPosition()).setUserName(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
}
添加更多Pojo
public class AddMorePojo
{
String userName,error;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
我的活动
public class MyActivity extends AppCompatActivity {
RecyclerView myRc;
ArrayList<AddMorePojo> arrayList = new ArrayList<>();
Button btnGetData;
AddMoreAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
myRc = (RecyclerView) findViewById(R.id.myRc);
btnGetData = (Button) findViewById(R.id.btnGetData);
myRc.setHasFixedSize(true);
myRc.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
AddMorePojo addMorePojo = new AddMorePojo();
addMorePojo.setUserName("");
arrayList.add(addMorePojo);
AddMorePojo addMorePojo1 = new AddMorePojo();
addMorePojo1.setUserName("");
addMorePojo1.setError("");
arrayList.add(addMorePojo1);
AddMorePojo addMorePojo2 = new AddMorePojo();
addMorePojo2.setUserName("");
addMorePojo2.setError("");
arrayList.add(addMorePojo2);
adapter = new AddMoreAdapter(this, arrayList);
myRc.setAdapter(adapter);
btnGetData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean flag=true;
ArrayList<AddMorePojo> pojoArrayList = adapter.getArrayList();
for (int i = 0; i < pojoArrayList.size(); i++) {
if (pojoArrayList.get(i).getUserName().isEmpty()) {
pojoArrayList.get(i).setError("Please enter userName");
flag=false;
}else {
// pojoArrayList.get(i).setError("");
}
}
adapter = new AddMoreAdapter(MyActivity.this, pojoArrayList);
myRc.setAdapter(adapter);
if (flag){
Toast.makeText(MyActivity.this, "Valid data", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MyActivity.this, "Invalid data", Toast.LENGTH_SHORT).show();
}
}
});
}
}
layout.activity_my
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/myRc"
android:layout_width="match_parent"
android:paddingBottom="50dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnGetData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:text="Get Data" />
</RelativeLayout>
添加更多布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="10dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/edtUname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter User Name" />
</LinearLayout>
</android.support.v7.widget.CardView>