RecyclerView shows previous values entered in an EditText in new rows

北城以北 提交于 2019-12-02 00:32:54

RecyclerView reuse views, in fact it only generate the as many as views that is visible on the screen. so it's expected if you can see a value you set for other rows

The solution would be set all attributes of the view that you are changing to default or whatever the row should present from your data set

So put addTextChangedListener insode ViewHolder constructor(you can get position by calling getAdapterPosition()) for better performance and set the editText value inside onBindViewHolder method from your data set

Your Activity Code:

ListView listview = (ListView) findViewById(R.id.list_view);
listview.setItemsCanFocus(true);
Adapter adapter = new Adapter (YourActivity.this, YourArrayList);
listview .setAdapter(adapter);

Adapter class

public class Adapter extends BaseAdapter {

// Declare Variables \\
Context mContext;
LayoutInflater inflater;
Activity act;
String[] temp;


public Adapter(Context context, ArrayList<String> list) {
    mContext = context;
    inflater = LayoutInflater.from(mContext);
    act = (Activity) context;
    //-------Temp String Array-------\\
    temp = new String[this.count];
    for (int i = 0; i < this.count; i++) {
        temp[i] = list.get(i);
    }
    //---------------------------\\

}

public class ViewHolder {
    TextView optionTitle;
    EditText optionText;
    int ref;
}

@Override
public int getCount() {
    return list.size;
}

@Override
public Object getItem(int position) {
    return temp[position];
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.lv_items_add_ques_options_mcq, null);
        holder.optionTitle = (TextView) view.findViewById(R.id.add_ques_opts_count_mcq_tv);
        holder.optionText = (EditText) view.findViewById(R.id.add_ques_opts_title_mcq_et);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    holder.ref = position;

    holder.optionTitle.setText(getCharForNumber(position) + ":");

    holder.optionText.setText(temp[position]);
    holder.optionText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            temp[holder.ref] = arg0.toString().trim();
        }
    });

    return view;
}

public void getList() {
    StaticValues.arrayListOptions = new ArrayList<String>(Arrays.asList(temp));
    StaticValues.arrayListOptionsCount = new ArrayList<String>();
    for (int i = 0; i < count; i++) {
        StaticValues.arrayListOptionsCount.add(String.valueOf(i+1));
        Log.e("err_al", StaticValues.arrayListOptions.get(i));
        Log.e("err_al", StaticValues.arrayListOptionsCount.get(i));
    }
}

private String getCharForNumber(int i) {
    char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    if (i > 25) {
        return null;
    }
    return Character.toString(alphabet[i]);
}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!