在RecyclerView中保存EditText内容

我有清单项目,EditText不知道会有多少项目。当我输入一些文本EditText然后向下滚动时RecyclerView,我遇到了问题,再向上滚动后,我的第一个文本就没有了EditText。


我想知道什么以及应该在哪里编写代码,以便在用户键入文本或完成键入文本时(我本想使用来完成此操作TextWatcher),EditText将文本保存到文件中(我将其保存在中)。外部存储中的txt文件)


我应该onCreate在活动的方法中还是在适配器类中或其他地方这样做吗?


这是一些代码


主要活动代码

 public class MainActivity extends Activity {


    RecyclerView mRecyclerView;

    MyAdapter mAdapter;

    String[] mDataSet= new String[20];

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // generating text for editText Views

        for (int i = 0; i<=19; i++){

        mDataSet[i]= "EditText n: "+i;


    }

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        mAdapter = new MyAdapter(mDataSet);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.setHasFixedSize(true);

    }

我的适配器代码

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


private String[] mDataset;



public static class ViewHolder extends RecyclerView.ViewHolder {

    // each data item is just a string in this case

    public EditText mEditText;


    public ViewHolder(View v) {

        super(v);


        mEditText = (EditText) v.findViewById(R.id.list_item_edittext);

    }

}


public MyAdapter(String[] myDataset) {

    mDataset = myDataset;

}


@Override

public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,

                                                 int viewType) {


    View v = LayoutInflater.from(parent.getContext())

            .inflate(R.layout.list_item, parent, false);


    ViewHolder vh = new ViewHolder(v);

    return vh;

}

没有此“ addtextChangedListener”,我的代码将无法正常工作,滚动时不保存编辑文本的内容。如果我添加此代码,则当我滚动离开屏幕并返回的所有editText视图时,会弄乱内容。


开满天机
浏览 2194回答 3
3回答

慕勒3428872

像这样在RecyclerView适配器中重写onViewRecycled方法:@Overridepublic void onViewRecycled(@NonNull ViewHolder holder) {&nbsp; &nbsp; mDataSet[holder.getAdapterPosition()] = holder.mEditText.getText().toString();}

白猪掌柜的

创建一个具有适配器数据大小的String数组。例如: String[] texts = new String[dataSize];在适配器内部的onBindViewHolder方法上,将TextChangedListener添加到Textview。例如:-@Override&nbsp; &nbsp; public void onBindViewHolder(Viewholder holder, int position) {//binding data from array&nbsp;&nbsp; &nbsp;holder.yourEditText.setText(texts [position]);&nbsp; &nbsp;holder.yourEditText.addTextChangedListener(new TextWatcher() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onTextChanged(CharSequence s, int start,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int before, int count) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //setting data to array, when changed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; texts [position] = s.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void beforeTextChanged(CharSequence s, int start,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int count, int after) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //blank&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void afterTextChanged(Editable s) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //blank&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android