ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 如何从片段验证recyclerview适配器TextInputEditText?

2019-09-18 03:27:47  阅读:282  来源: 互联网

标签:android-viewholder android android-fragments android-recyclerview android-textin


我有一个片段,其中包含recyclerview.此回收站视图使用包含视图集模式的适配器进行管理.
在片段中我有一个“保存”按钮,此按钮必须检查列表中没有TextInputEditText为空.

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

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

我还读到,在onBindViewHolder方法之外访问视图是不太好的做法.

任何人都可以帮我搞定吗?这看起来并不困难,但却让我浪费了很多时间.

我只想点击片段中的保存按钮,检查我的recyclerview中的项目列表中没有TextInputEditText是空的

谢谢

解决方法:

试试这个

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-viewholder,android,android-fragments,android-recyclerview,android-textin
来源: https://codeday.me/bug/20190918/1810334.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有