如何获取Recyclerview项目';Android的价值是什么?

如何获取Recyclerview项目';Android的价值是什么?,android,android-recyclerview,Android,Android Recyclerview,Recyclerview有4项,即:;文本视图1、文本视图2、编辑文本1和复选框1。 Recyclerview也有24行。EditText在初始阶段不可见,只有在用户选中相应的复选框时才可见。EditText只接受数字 到目前为止,该应用程序运行良好 现在我需要获取所有EditText的值,并需要在另一个Textview上显示它,该Textview不在Recyclerview部分中 代码示例 ExamFragment.java public class ExamFragment extends

Recyclerview有4项,即:;文本视图1、文本视图2、编辑文本1和复选框1。 Recyclerview也有24行。EditText在初始阶段不可见,只有在用户选中相应的复选框时才可见。EditText只接受数字

到目前为止,该应用程序运行良好

现在我需要获取所有EditText的值,并需要在另一个Textview上显示它,该Textview不在Recyclerview部分中

代码示例

ExamFragment.java

public class ExamFragment extends Fragment  {


    RecyclerView recyclerView;
    ExamFragmentAdapter adapter;
    ArrayList<tblChapters> datalistChapters = new ArrayList<>();
    TextView txtQcount,txtQCounttotal;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_exam, container, false);

        txtQCounttotal=(TextView) v.findViewById(R.id.txtQCounttotal);
        txtQcount=(TextView) v.findViewById(R.id.txtQCount);
        recyclerView=(RecyclerView)v.findViewById(R.id.recycler_view);
        conn = new ConnectionClass(); //connection initialisation
        datalistChapters = conn.getChaptersAndCount(modeid, subjectid);
        adapter = new ExamFragmentAdapter(datalistChapters, getActivity());
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
        return v;
}

}
public class ExamFragmentAdapter extends RecyclerView.Adapter<ExamFragmentAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    MyViewHolder holder;
    Context mContext;


    ArrayList<tblChapters> chapterList=new ArrayList<>();
    public ExamFragmentAdapter(ArrayList<tblChapters> chapterList, Context context) {
        inflater = LayoutInflater.from(context);
        this.chapterList = chapterList;
        mContext=context;

    }
    @Override
    public ExamFragmentAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.chapter_list_row, parent, false);
        holder = new MyViewHolder(view, new MyCustomEditTextListener());
        return holder;

    }

    @Override
    public void onBindViewHolder(final ExamFragmentAdapter.MyViewHolder holder, final int position) {
        holder.title.setTextColor(Color.BLACK);
        holder.slno.setTextColor(Color.BLACK);
        holder.noOfQst.setTextColor(Color.BLACK);
        holder.noOfQst.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        holder.noOfQst.setGravity(Gravity.CENTER);
        holder.title.setText(chapterList.get(position).getTitle()); // Setting Chapter title
        holder.slno.setText(String.valueOf(position + 1)+"."); //Setting sl no

        holder._myCustomEditTextListener.updatePosition(position);

        holder.noOfQst.setText(chapterList.get(position).getNoofQstns()); //Setting no of qstn

        if (chapterList.get(position).isVisibled()) {
            holder.noOfQst.setVisibility(View.VISIBLE);
        } else {
            holder.noOfQst.setVisibility(View.INVISIBLE);
        }

        //in some cases, it will prevent unwanted situations
        holder.cbox.setOnCheckedChangeListener(null);

        //if true, your checkbox will be selected, else unselected
        holder.cbox.setChecked(chapterList.get(position).isSelected());

        holder.cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //set your object's last status
                chapterList.get(position).setSelected(isChecked);
                chapterList.get(position).setVisibled(isChecked);


                //if checkbox checked display EditText(No of qstns), else hide it.
                if (holder.cbox.isChecked()) {
                    holder.noOfQst.setVisibility(View.VISIBLE);
                    holder.noOfQst.requestFocus();
                    holder.noOfQst.setText("10");
                    chapterList.get(position).setNoofQstns(holder.noOfQst.getText().toString());
                  /*  txtQcount.setText("0");
                    if (txtQcount.getText().toString().equals("")) {
                        txtQcount.setText("0");
                    }
                    txtQcount.setText(Integer.valueOf(txtQcount.getText().toString())+Integer.parseInt(holder.noOfQst.getText().toString()));*/
                }
                else {
                    holder.noOfQst.setVisibility(View.INVISIBLE);
                    holder.noOfQst.setText(""); //remove entered value when uncheck
                    chapterList.get(position).setNoofQstns("");
                }
            }
        });

    }

    // we make TextWatcher to be aware of the position it currently works with
    // this way, once a new item is attached in onBindViewHolder, it will
    // update current position MyCustomEditTextListener, reference to which is kept by ViewHolder
    private class MyCustomEditTextListener implements TextWatcher

    {
        private int position;
        private String oldval;


        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            chapterList.get(position).setNoofQstns(charSequence.toString());
            int j = i;
            j = i2;
            j = i3;
        }

        @Override
        public void afterTextChanged(Editable editable) {


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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        CheckBox cbox;
        TextView slno;
        EditText noOfQst;
        public MyCustomEditTextListener _myCustomEditTextListener;
        public MyViewHolder(View itemView,MyCustomEditTextListener myCustomEditTextListener) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.txtTitle);
            cbox = (CheckBox) itemView.findViewById(R.id.cboxChapter);
            slno = (TextView) itemView.findViewById(R.id.txtRowSlno);
            noOfQst = (EditText) itemView.findViewById(R.id.etNoOfQstns);

            this._myCustomEditTextListener = myCustomEditTextListener;

            try {
                if (noOfQst.getVisibility() == View.VISIBLE) {
                    holder.noOfQst.setVisibility(View.INVISIBLE);
                    //adding textchange listener to no of qstn(EditText)
                    noOfQst.addTextChangedListener(myCustomEditTextListener);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
公共类ExamFragment扩展了片段{
回收视图回收视图;
ExamFragmentAdapter适配器;
ArrayList datalistChapters=新的ArrayList();
text查看txtQcount,txtQCounttotal;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图v=充气机。充气(R.layout.fragment\u检查,容器,错误);
txtQCounttotal=(TextView)v.findViewById(R.id.txtQCounttotal);
txtQcount=(TextView)v.findviewbyd(R.id.txtQcount);
recyclerView=(recyclerView)v.findViewById(R.id.recycler\u视图);
conn=新连接类();//连接初始化
datalistChapters=conn.getChapters和Count(modeid,subjectid);
适配器=新的ExamFragmentAdapter(datalistChapters,getActivity());
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
setItemAnimator(新的DefaultItemAnimator());
recyclerView.setAdapter(适配器);
返回v;
}
}
ExamFragmentAdapter.java

public class ExamFragment extends Fragment  {


    RecyclerView recyclerView;
    ExamFragmentAdapter adapter;
    ArrayList<tblChapters> datalistChapters = new ArrayList<>();
    TextView txtQcount,txtQCounttotal;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_exam, container, false);

        txtQCounttotal=(TextView) v.findViewById(R.id.txtQCounttotal);
        txtQcount=(TextView) v.findViewById(R.id.txtQCount);
        recyclerView=(RecyclerView)v.findViewById(R.id.recycler_view);
        conn = new ConnectionClass(); //connection initialisation
        datalistChapters = conn.getChaptersAndCount(modeid, subjectid);
        adapter = new ExamFragmentAdapter(datalistChapters, getActivity());
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
        return v;
}

}
public class ExamFragmentAdapter extends RecyclerView.Adapter<ExamFragmentAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    MyViewHolder holder;
    Context mContext;


    ArrayList<tblChapters> chapterList=new ArrayList<>();
    public ExamFragmentAdapter(ArrayList<tblChapters> chapterList, Context context) {
        inflater = LayoutInflater.from(context);
        this.chapterList = chapterList;
        mContext=context;

    }
    @Override
    public ExamFragmentAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.chapter_list_row, parent, false);
        holder = new MyViewHolder(view, new MyCustomEditTextListener());
        return holder;

    }

    @Override
    public void onBindViewHolder(final ExamFragmentAdapter.MyViewHolder holder, final int position) {
        holder.title.setTextColor(Color.BLACK);
        holder.slno.setTextColor(Color.BLACK);
        holder.noOfQst.setTextColor(Color.BLACK);
        holder.noOfQst.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        holder.noOfQst.setGravity(Gravity.CENTER);
        holder.title.setText(chapterList.get(position).getTitle()); // Setting Chapter title
        holder.slno.setText(String.valueOf(position + 1)+"."); //Setting sl no

        holder._myCustomEditTextListener.updatePosition(position);

        holder.noOfQst.setText(chapterList.get(position).getNoofQstns()); //Setting no of qstn

        if (chapterList.get(position).isVisibled()) {
            holder.noOfQst.setVisibility(View.VISIBLE);
        } else {
            holder.noOfQst.setVisibility(View.INVISIBLE);
        }

        //in some cases, it will prevent unwanted situations
        holder.cbox.setOnCheckedChangeListener(null);

        //if true, your checkbox will be selected, else unselected
        holder.cbox.setChecked(chapterList.get(position).isSelected());

        holder.cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //set your object's last status
                chapterList.get(position).setSelected(isChecked);
                chapterList.get(position).setVisibled(isChecked);


                //if checkbox checked display EditText(No of qstns), else hide it.
                if (holder.cbox.isChecked()) {
                    holder.noOfQst.setVisibility(View.VISIBLE);
                    holder.noOfQst.requestFocus();
                    holder.noOfQst.setText("10");
                    chapterList.get(position).setNoofQstns(holder.noOfQst.getText().toString());
                  /*  txtQcount.setText("0");
                    if (txtQcount.getText().toString().equals("")) {
                        txtQcount.setText("0");
                    }
                    txtQcount.setText(Integer.valueOf(txtQcount.getText().toString())+Integer.parseInt(holder.noOfQst.getText().toString()));*/
                }
                else {
                    holder.noOfQst.setVisibility(View.INVISIBLE);
                    holder.noOfQst.setText(""); //remove entered value when uncheck
                    chapterList.get(position).setNoofQstns("");
                }
            }
        });

    }

    // we make TextWatcher to be aware of the position it currently works with
    // this way, once a new item is attached in onBindViewHolder, it will
    // update current position MyCustomEditTextListener, reference to which is kept by ViewHolder
    private class MyCustomEditTextListener implements TextWatcher

    {
        private int position;
        private String oldval;


        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            chapterList.get(position).setNoofQstns(charSequence.toString());
            int j = i;
            j = i2;
            j = i3;
        }

        @Override
        public void afterTextChanged(Editable editable) {


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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        CheckBox cbox;
        TextView slno;
        EditText noOfQst;
        public MyCustomEditTextListener _myCustomEditTextListener;
        public MyViewHolder(View itemView,MyCustomEditTextListener myCustomEditTextListener) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.txtTitle);
            cbox = (CheckBox) itemView.findViewById(R.id.cboxChapter);
            slno = (TextView) itemView.findViewById(R.id.txtRowSlno);
            noOfQst = (EditText) itemView.findViewById(R.id.etNoOfQstns);

            this._myCustomEditTextListener = myCustomEditTextListener;

            try {
                if (noOfQst.getVisibility() == View.VISIBLE) {
                    holder.noOfQst.setVisibility(View.INVISIBLE);
                    //adding textchange listener to no of qstn(EditText)
                    noOfQst.addTextChangedListener(myCustomEditTextListener);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
公共类ExamFragmentAdapter扩展了RecyclerView.Adapter{
私人充气机;
我的视窗持有人;
语境;
ArrayList chapterList=新的ArrayList();
公共ExamFragmentAdapter(ArrayList章节列表,上下文){
充气器=充气器。从(上下文);
this.chapterList=chapterList;
mContext=上下文;
}
@凌驾
public ExamFragmentAdapter.MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图=充气机。充气(R.layout.chapter\u list\u row,parent,false);
holder=新的MyViewHolder(视图,新的MyCustomEditTextListener());
报税表持有人;
}
@凌驾
BindViewHolder上的公共无效(期末考试FragmentAdapter.MyViewHolder,最终整数位置){
holder.title.setTextColor(Color.BLACK);
holder.slno.setTextColor(颜色:黑色);
holder.noOfQst.setTextColor(颜色为黑色);
支架noOfQst.SettexSize(TypedValue.COMPLEX\u UNIT\u SP,14);
保持架无固定重力(重心);
holder.title.setText(chapterList.get(position.getTitle());//设置章节标题
holder.slno.setText(String.valueOf(position+1)+“);//设置sl号
holder.\u myCustomEditTextListener.updatePosition(位置);
holder.noOfQst.setText(chapterList.get(position.getNoofQstns());//设置qstn的编号
if(chapterList.get(position).isVisibled()){
holder.noofkst.setVisibility(View.VISIBLE);
}否则{
holder.noofkst.setVisibility(视图不可见);
}
//在某些情况下,它可以防止不必要的情况
holder.cbox.setOnCheckedChangeListener(空);
//如果为true,将选中您的复选框,否则取消选中
holder.cbox.setChecked(chapterList.get(position.isSelected());
holder.cbox.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
//设置对象的最后状态
章节列表.get(位置).setSelected(已检查);
章节列表.get(位置).setVisibled(已检查);
//如果复选框选中“显示编辑文本”(QSTN的编号),则将其隐藏。
if(holder.cbox.isChecked()){
holder.noofkst.setVisibility(View.VISIBLE);
holder.noOfQst.requestFocus();
持有人:noOfQst.setText(“10”);
chapterList.get(position.setNoofQstns(holder.noOfQst.getText().toString());
/*txtQcount.setText(“0”);
if(txtQcount.getText().toString().equals(“”){
txtQcount.setText(“0”);
}
txtQcount.setText(Integer.valueOf(txtQcount.getText().toString())+Integer.parseInt(holder.noOfQst.getText().toString())*/
}
否则{
holder.noofkst.setVisibility(视图不可见);
holder.noOfQst.setText(“”;//取消选中时删除输入的值
chapterList.get(position).setNoofQstns(“”);
}
}
});
}
//我们让TextWatcher知道它当前使用的位置
//这样,一旦在onBindViewHolder中附加了一个新项目,它将
//更新当前位置MyCustomEditTextListener,ViewHolder保留对其的引用
私有类MyCustomEditTextListener实现TextWatcher
{
私人职位;
私有字符串oldval;
公共无效更新位置(整数位置){
这个位置=位置;
}
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i2,int i3){
//无操作
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i2,int i3){
chapterList.get(position.setNoofQstns(charSequence.toString());
int j=i;
j=i2;
j=i3;
}
@凌驾
公共无效后文本更改(可编辑