如何在Android中获取Recyclerview物品的价值? [英] How to get Recyclerview item's value in Android?

查看:57
本文介绍了如何在Android中获取Recyclerview物品的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Recyclerview有4个项目,即; textView1,textView2,EditText1和Checkbox1. Recyclerview也有24行. EditText在初始阶段是不可见的,只有当用户选中相应的复选框时,它才是可见的. EditText仅接受数字.

Recyclerview has 4 items i.e; textView1, textView2, EditText1 and Checkbox1. Recyclerview has got 24 rows as well. EditText is invisible on initial stage thenit will be visible only when the corresponding checkbox checked by the user. EditText accepts only numbers.

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

The app is working fine so far this much.

现在我需要获取所有EditText的值并将其显示在Recyclerview之外的另一个Textview上吗?

Now I need to get value of all EditTexts and need to display it on another Textview which is not in the part of Recyclerview?

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

}

ExamFragmentAdapter.java

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

        }
    }

推荐答案

您可以注册

You can register an AdapterDataObserver for the recycler view adapter. For each item in the adapter when it's updated (user updates the EditText), the adapter can call notifyItemChanged(int position, Object payload). the observer will receive the payload in its onItemRangeChanged (int positionStart, int itemCount, Object payload) callback, you can pass whatever you want in the payload object so that you accumulate the values of all the EditTexts.

您还可以通过 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆