Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从片段验证recyclerview适配器TextInputItemText?_Android_Android Fragments_Android Recyclerview_Android Viewholder_Android Textinputedittext - Fatal编程技术网

Android 如何从片段验证recyclerview适配器TextInputItemText?

Android 如何从片段验证recyclerview适配器TextInputItemText?,android,android-fragments,android-recyclerview,android-viewholder,android-textinputedittext,Android,Android Fragments,Android Recyclerview,Android Viewholder,Android Textinputedittext,我有一个片段,其中包含一个recyclerview。此recyclerview由包含viewholder模式的适配器管理。 在片段中,我有一个“保存”按钮,这个按钮必须检查列表中没有TextInputItemText为空 问题是我无法访问适配器的TextInputItemText。只能通过“onBindViewHolder”方法访问ViewHolder。 我试图以某种方式从fragment访问: for (int i = 0; i < employeeList.size(); i++) {

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

问题是我无法访问适配器的TextInputItemText。只能通过“onBindViewHolder”方法访问ViewHolder。 我试图以某种方式从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);
for(int i=0;i
通过这样做,我成功地在TextInputLayout中标记了错误。我还能够获得TextInputInputText的文本。一切似乎都正常,但是

当列表更改时出现问题,我需要调用
notifyDataSetChanged

findViewHolderForAdapterPosition
在列表的某些位置返回null

我在一些帖子中读到,调用该方法是不安全的

我还了解到,在
onBindViewHolder
方法之外访问
viewholder
不是一个好的做法

有人能帮我弄到这个吗?这似乎并不难,但它给我造成了极大的时间浪费

我只想单击片段中的“保存”按钮,检查my
recyclerview中的项目列表中是否没有
TextInputItemText
为空


谢谢

是的,你是对的,这并不难

@Override
public void onBindView(Object object, int pos) {

        Employee employee = (Employee) object;
        editText.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) {
                employee.setName(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
在“保存”按钮上,从列表中获取值。

试试这个

Addmore适配器

我的活动

公共类MyActivity扩展了AppCompatActivity{
回收视图myRc;
ArrayList ArrayList=新的ArrayList();
按钮btnGetData;
添加更多适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
myRc=(RecyclerView)findViewById(R.id.myRc);
btnGetData=(按钮)findViewById(R.id.btnGetData);
myRc.setHasFixedSize(真);
myRc.setLayoutManager(新的LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
AddMorePojo AddMorePojo=新的AddMorePojo();
addMorePojo.setUserName(“”);
arrayList.add(addMorePojo);
AddMorePojo addMorePojo1=新的AddMorePojo();
addMorePojo1.setUserName(“”);
addMorePojo1.setError(“”);
arrayList.add(addMorePojo1);
AddMorePojo addMorePojo2=新的AddMorePojo();
addMorePojo2.setUserName(“”);
addMorePojo2.setError(“”);
arrayList.add(addMorePojo2);
adapter=新的AddMoreAdapter(此为arrayList);
myRc.setAdapter(适配器);
btnGetData.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
布尔标志=真;
ArrayList pojoArrayList=adapter.getArrayList();
对于(int i=0;i
layout.activity\u my


addmorelayout


例如,您可以在模型中使用布尔字段

class TestData{

 private boolean isTextEmpty;

 private String textData;

 public void setTextEmpty(boolean isTrue){
    isTextEmpty = isTrue;
 }

 public boolean isTextEmpty(){
    return isTextEmpty;
 }

 public void setTextData(String data){
    textData = data;
 }

 public void getData(){
    return textData;
 }

 ........
 ///other fields goes here
}
现在在列表中使用此模型并将其提供给适配器

    public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {

        ArrayList<TestData> arrayList;
        boolean isSubmitClick = false;

        public AddMoreAdapter(ArrayList<TestData> arrayList) {
            this.arrayList = arrayList;
        }

        @Override
        public TestAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(context).inflate(R.layout.test_data_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(TestAdapter.ViewHolder holder, int position) {
            TestData data = arrayList.get(holder.getAdapterPosition());
            if (holder.editText.getText().toString()==null) {
                if(isSubmitClick){
                 holder.inputText.setError("please enter a valid input");
                }   
                data.setTextEmpty(true);
            } else {
                data.setTextEmpty(false);
                //do something else
            }
        }

        @Override
        public int getItemCount() {
            return arrayList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextInputLayout textInput;
            EditText editName;

            public ViewHolder(View itemView) {
                super(itemView);

                editName = itemView.findViewById(R.id.etTest);
                textInput = itemView.findViewById(R.id.tilTest);

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

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                         arrayList.get(getAdapterPosition()).setTestData(editable.toString());
                    }
                });

            }
        }

        public void setSubmitClick(boolean isTrue){
          this.isSubmitClick = isTrue; 
        }

}

最初,recyclerview中的所有edittext都是空的?在某些特定情况下,recyclerview可能是空的,尽管它不是空的normal@NileshRathod给出了我也在想的答案,只是检查一下。谢谢你的回答。这似乎毫无疑问是正确的答案。我看到的问题是arrayList不是可从viewholder访问class@jlopez没有明白你的意思吗?什么意思
arrayList不能从viewholder类访问
我的viewholder是静态类。删除静态属性有问题吗?@jlopez我认为从viewholder中删除静态属性没有问题
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();
                }
            }

        });
    }


}
<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>
class TestData{

 private boolean isTextEmpty;

 private String textData;

 public void setTextEmpty(boolean isTrue){
    isTextEmpty = isTrue;
 }

 public boolean isTextEmpty(){
    return isTextEmpty;
 }

 public void setTextData(String data){
    textData = data;
 }

 public void getData(){
    return textData;
 }

 ........
 ///other fields goes here
}
    public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {

        ArrayList<TestData> arrayList;
        boolean isSubmitClick = false;

        public AddMoreAdapter(ArrayList<TestData> arrayList) {
            this.arrayList = arrayList;
        }

        @Override
        public TestAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(context).inflate(R.layout.test_data_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(TestAdapter.ViewHolder holder, int position) {
            TestData data = arrayList.get(holder.getAdapterPosition());
            if (holder.editText.getText().toString()==null) {
                if(isSubmitClick){
                 holder.inputText.setError("please enter a valid input");
                }   
                data.setTextEmpty(true);
            } else {
                data.setTextEmpty(false);
                //do something else
            }
        }

        @Override
        public int getItemCount() {
            return arrayList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextInputLayout textInput;
            EditText editName;

            public ViewHolder(View itemView) {
                super(itemView);

                editName = itemView.findViewById(R.id.etTest);
                textInput = itemView.findViewById(R.id.tilTest);

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

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                         arrayList.get(getAdapterPosition()).setTestData(editable.toString());
                    }
                });

            }
        }

        public void setSubmitClick(boolean isTrue){
          this.isSubmitClick = isTrue; 
        }

}
public void isDataValid(){

  for(int i = 0; i<arrayList.size(); i++){
     TestData data = arrayList.get(i);
     if(data.isTextEmpty()){
        adapter.notifyItemChanged(i);
        return;
     }
  }

  //do something here as all data is valid
  callServer();
}