Recyclerview有4个项目,即; textView1,textView2,EditText1和Checkbox1。 Recyclerview也有24行。 EditText在初始阶段是不可见的,只有在用户选中相应的复选框时才会显示。 EditText只接受数字。
到目前为止,应用程序运行良好。
现在我需要获取所有EditTexts的值,并且需要在另一个不在Recyclerview部分的Textview中显示它吗?
Recyclerview Screenshot- Output Link
代码示例。
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();
}
}
}
答案 0 :(得分:0)
您可以为回收站视图适配器注册AdapterDataObserver。 对于适配器中更新的每个项目(用户更新EditText),适配器可以调用notifyItemChanged(int position, Object payload)。观察者将在其onItemRangeChanged (int positionStart, int itemCount, Object payload)回调中接收有效负载,您可以在有效负载对象中传递任何所需的内容,以便累积所有EditTexts的值。
取消注册Activity / Fragment onStop中的观察者