在 RecyclerView 中保存 EditText 内容

编程入门 行业动态 更新时间:2024-10-18 16:50:43
本文介绍了在 RecyclerView 中保存 EditText 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有 EditText 的列表项,我不知道会有多少项.当我在 EditText 中输入一些文本,然后向下滚动 RecyclerView 时遇到问题,再次向上滚动后,我的第一个 中没有文本编辑文本.

I have list item with EditText in it, I don't know how many items there will be. I have a problem when I enter some text in EditText, and then scroll down a RecyclerView, after I've scroll up again there is no text in my first EditText.

我想知道我应该在什么地方编写代码,以便在用户输入或完成输入时(我想用 TextWatcher 来完成)在 EditText 文本被保存到一个文件中(我将它保存在外部存储的 .txt 文件中)

I am wondering what, and where should I write code so that while the user is typing or finished typing (I was thinking to do that with a TextWatcher) in the EditText the text gets saved into into a file (I'll save it in a .txt file in the external storage)

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

Am I supposed to do so in the onCreate method of the activity or in the adapter class or elsewhere?

这是一些代码

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; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { holder.mEditText.setText(mDataset[position]); //without this addtextChangedListener my code works fine ovbiusly // not saving the content of the edit Text when scrolled // If i add this code then when i scroll all textView that go of screen // and than come back in have messed up content holder.mEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //setting data to array, when changed // this is a semplified example in the actual app i save the text // in a .txt in the external storage mDataset[position] = s.toString(); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } @Override public int getItemCount() { return mDataset.length; }

没有这个addtextChangedListener",我的代码工作正常,显然滚动时不保存编辑文本的内容.如果我添加此代码,当我滚动所有离开屏幕然后返回的 editText 视图时,内容会变得混乱.

without this "addtextChangedListener" my code works fine obviusly not saving the content of the edit Text when scrolled. If i add this code, when i scroll all editText views that go off screen and than come back in have messed up content.

推荐答案

您的解决方案的主要问题是在 onBindViewHolder 中分配和分配 TextWatcher,这是一项昂贵的操作,会在快速滚动期间引入延迟,并且似乎还会干扰确定在 mAdapter 中更新什么位置.

The major problem with your solution is allocating and assigning TextWatcher in onBindViewHolder which is an expensive operation that will introduce lags during fast scrolls and it also seems to interfere with determining what position to update in mAdapter.

在 onCreateViewHolder 中进行所有操作是更可取的选择.这是完整的测试工作解决方案:

Making all operations in onCreateViewHolder is a more preferable option. Here is the complete tested working solution:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private String[] mDataset; 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_edittext, parent, false); // pass MyCustomEditTextListener to viewholder in onCreateViewHolder // so that we don't have to do this expensive allocation in onBindViewHolder ViewHolder vh = new ViewHolder(v, new MyCustomEditTextListener()); return vh; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { // update MyCustomEditTextListener every time we bind a new item // so that it knows what item in mDataset to update holder.myCustomEditTextListener.updatePosition(holder.getAdapterPosition()); holder.mEditText.setText(mDataset[holder.getAdapterPosition()]); } @Override public int getItemCount() { return mDataset.length; } public static class ViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case public EditText mEditText; public MyCustomEditTextListener myCustomEditTextListener; public ViewHolder(View v, MyCustomEditTextListener myCustomEditTextListener) { super(v); this.mEditText = (EditText) v.findViewById(R.id.editText); this.myCustomEditTextListener = myCustomEditTextListener; this.mEditText.addTextChangedListener(myCustomEditTextListener); } } // 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; 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) { mDataset[position] = charSequence.toString(); } @Override public void afterTextChanged(Editable editable) { // no op } } }

更多推荐

在 RecyclerView 中保存 EditText 内容

本文发布于:2023-10-08 01:26:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   RecyclerView   EditText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!