android - 如何从 fragment 验证 recyclerview 适配器 TextInputEditText?

标签 android android-fragments android-recyclerview android-viewholder android-textinputedittext

我有一个 fragment ,其中包含一个 recyclerview。这个 recyclerview 由一个包含 viewholder 模式的适配器管理。
在 fragment 中我有一个“保存”按钮,这个按钮必须检查列表中没有 TextInputEditText 为空。

问题是我无法访问适配器的 TextInputEditText。 ViewHolder 只能通过“onBindViewHolder”方法访问。
我试图以某种方式从 fragment 访问:

for (int i = 0; i < employeeList.size(); i++) {
            View employeeView = recyclerEmployees.findViewHolderForAdapterPosition(i).itemView;
            if (employeeView != null) {
                TextInputLayout textInputLayoutName = employeeView.findViewById(R.id.tilName);
                TextInputEditText textInputEditTextName = employeeView.findViewById(R.id.tieName);

通过这样做,我设法在 TextInputLayout 中标记了错误。我还能够获取 TextInputEditText 的文本。一切似乎都奏效了,但是……

当列表更改并且我需要通过调用 notifyDataSetChanged 刷新适配器时出现问题。
findViewHolderForAdapterPosition在列表的某些位置返回 null。

我在一些帖子中读到调用该方法是不安全的
https://stackoverflow.com/a/32840927/1484779

我还读到访问 viewholder 不是一个好习惯。外onBindViewHolder方法。

谁能帮我得到这个?这看起来并不难,但它使我浪费了大量时间。

我只想点击 fragment 中的保存按钮并检查是否没有 TextInputEditText在我的 recyclerview 中的项目列表中为空

谢谢

最佳答案

试试这个

AddMoreAdapter


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) {

                }
            });


        }
    }
}

AddMorePojo


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;
    }
}

MyActivity


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>

addmorelayout


<?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>

关于android - 如何从 fragment 验证 recyclerview 适配器 TextInputEditText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51454613/

相关文章:

android ListView mListView.getChildAt(i) 为null,如何解决?

android - setIconified(false) 强制关闭我的应用

android - recyclerview header,从Activity/Fragment中的header访问对象

java - Android Fragment 的 onClickListener 未调用

java - 连接到模拟器服务器的多个模拟器客户端

android - 通过蓝牙、Android 传输视频

android - List Adapter Diff Util 不更新 Recyclerview 中的列表项

java.lang.ClassCastException : android. widget.RelativeLayout$LayoutParams 无法转换为 android.support.v7.widget.RecyclerView$LayoutParams

java - 以编程方式在 RecyclerView 项目(项目中的某个元素)上设置背景颜色

java - 按钮无法点击