如何从MainActivity中的RecyclerView创建的所有EditTexts中检索值?
在我的RecyclerView适配器中,我正在扩展我的内部类:
public class MyPersonalAdapter extends RecyclerView.Adapter<MyPersonalAdapter.MyPersonalViewHolder>
我在该内部类中获得了对EditText的引用:
class MyPersonalViewHolder extends RecyclerView.ViewHolder {
TextView numberTextView;
EditText nameEditText;
public MyPersonalViewHolder(View itemView) {
super(itemView);
numberTextView = (TextView) itemView.findViewById(R.id.tv_number);
nameEditText = (EditText) itemView.findViewById(R.id.et_name);
}
}
在我想要使用的MainActivity中:
for (int i = 0; i < count; i++) {
String name = "Somehow get that name";
cv.put(MyContract.MyEntry.COLUMN_NAME, "name");
}
搞定了,这是编辑过的代码:
mAdapter = new MyClassAdapter(this, mDataset.size);
mRecyclerView.setAdapter(mAdapter);
mRecyclerview.setItemViewCacheSize(mDataset.size());
List<ContentValues> list = new ArrayList<>();
for (int i = 0; i < mDataset.size(); i++) {
View view = recyclerView.getChildAt(i);
EditText nameEditText = (EditText) view.findViewById(R.id.et_name);
String name = nameEditText.getText().toString();
ContentValues cv = new ContentValues();
cv.put(MyContract.MyEntry.COLUMN_NAME, name);
list.add(cv)
}
// I encapsulated this in a try catch
for (ContentValues c:list) {
mDb.insert(MyClassContract.MyClassEntry.TABLE_NAME, null, c);
}
试试这个:
for(int i=0;i<adapter.getItemCount();i++){
MyPersonalViewHolder viewHolder= (MyPersonalViewHolder )
mRecyclerView.findViewHolderForAdapterPosition(i);
EditText editText=viewHolder.nameEditText;
}
在recyclerview适配器中的bindview方法中实现addTextChangedListener。
每次在任何单元格中修改edittext文本时,修改该位置的arraylist字符串。
稍后当你需要整个arraylist时,只需通过任何公共getmethod从适配器类发送回来。
这应该足够了。
我在Adapter类中创建了一个getData函数。
public String getData()
{
String s;
s=txt1.getText().toString();
return s;
}
然后在我的MainActivity中
public void onSave(View view) {
String[] s=new String[length];
for(int i=0;i<ad.getItemCount();i++)
{
s[i]=ad.getData().toString();
}
}
这样,您可以在阵列中保存编辑文本条目。
这对我有用:
mySetEnabled
是我在viewHolder中实现的一种方法。
if(mRecView!=null) {
int size=mRecView.getChildCount();
for (int i = 0; i < size; i++) {
myAdapter.myViewHolder wordView = (myAdapter.myViewHolder)mRecView.findViewHolderForLayoutPosition(i);
if(wordView!=null)
wordView.mySetEnabled(state);
}
}
试试这个,
class MyPersonalViewHolder扩展了RecyclerView.ViewHolder {
TextView numberTextView;
EditText nameEditText;
public MyPersonalViewHolder(View itemView) {
super(itemView);
numberTextView = (TextView) itemView.findViewById(R.id.tv_number);
nameEditText = (EditText) itemView.findViewById(R.id.et_name);
nameEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
values.add(getAdapterPosition(),s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
另外,定义一个函数
public String getValue(int position){
return values.get(position);
}
现在getValue可以从MainActivity调用。
//So the other day I spend full day to get data(list of edittext) from recyclerview to activity when i press
button in activity
//perform onclick of button
这是适配器中的代码,没有使用textchange listener ..所以我不得不使用textchange listener和setOnfoucusChange(100%工作)
holder.mComment.setOnFocusChangeListener(new
View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
String data=holder.mComment.getText().toString();
commentList[position]=data;
}
if(position==mList.size()-1){
holder.mComment.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int i, int i1, int i2) {
commentList[position]=s.toString();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
});
Intent intent = new Intent("mrn_intent");
Bundle args = new Bundle();
args.putSerializable("comment_list",(Serializable)commentList);
args.putSerializable("rating_list", (Serializable) mRatingList);
intent.putExtra("BUNDLE_COMMENT",args);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
并在活动中编写以下代码
Bundle args2 = intent.getBundleExtra("BUNDLE_COMMENT");
if(args2!=null){
list = (String[]) args2.getSerializable("comment_list");
Log.d(TAG, "onReceive: list+++=>>>>>>"+list);
}