RecyclerView显示在新行中在EditText中输入的先前值

时间:2017-12-20 01:59:51

标签: android android-recyclerview recycler-adapter addtextchangedlistener

我正在创建一个Android应用,其中我使用recyclerView并且recyclelerView行有editText

这是我的ReadingAdapter班级

public class ReadingAdapter extends RecyclerView.Adapter<ReadingAdapter.ViewHolder>  implements AdapterView.OnItemSelectedListener {

    Context context;
    String valOpenReading, valClosReading, valConsumption;
    private List<ReadingData> readingList;
    static String[] arrValOpenRead, arrValClosRead, arrValConsumption;
    public ReadingAdapter(Context context, List<ReadingData> readingList) {
        this.context = context;
        this.readingList = readingList;

        arrValOpenRead = new String[readingList.size()];
        arrValClosRead = new String[readingList.size()];
        arrValConsumption = new String[readingList.size()];
    }

    @Override
    public ReadingAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reading_sheet_layout, parent, false);
        return new ReadingAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ReadingAdapter.ViewHolder holder, final int position) {
        ReadingData tempData = readingList.get(position);
        holder.pdtName.setText(tempData.pdtName);
        holder.keyId.setText("Key "+tempData.keyId);

        holder.etClosRead.addTextChangedListener(new TextWatcher() {
            boolean ignore = false;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                if (ignore)
                    return;
                ignore = true;
                valOpenReading = holder.etOpenRead.getText().toString();
                arrValOpenRead[position] = valOpenReading;
                valClosReading = s.toString().equals("") ? "0": s.toString();
                arrValClosRead[position] = valClosReading;
                if (!valOpenReading.equals("")) {
                    if (Integer.parseInt(valClosReading) < Integer.parseInt(valOpenReading)) {
                        Toast.makeText(context, "Check once! closing reading should be more than opening reading!", Toast.LENGTH_LONG).show();
                        valConsumption = "0";
                        holder.consumption.setText("");
                    } else {
                        valConsumption = (Integer.parseInt(valClosReading) - Integer.parseInt(valOpenReading))+"";
                        arrValConsumption[position] = valConsumption;
                        holder.consumption.setText(valConsumption);
                    }
                } else
                    Toast.makeText(context, "Please fill the opening reading!", Toast.LENGTH_SHORT).show();
                ignore = false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return readingList.size();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView pdtName, keyId, consumption;
        EditText etOpenRead, etClosRead;
        public ViewHolder(View view) {
            super(view);
            pdtName = (TextView)view.findViewById(R.id.txt_list_pdt_supp);
            keyId = (TextView)view.findViewById(R.id.key_set);
            etOpenRead = (EditText)view.findViewById(R.id.open_val_set);
            etClosRead = (EditText)view.findViewById(R.id.clos_val_set);
            consumption = (TextView)view.findViewById(R.id.consumption_val);
        }
    }
}

这是我的ReadingData.java

public class ReadingData {
    String pdtName, keyId, openReading, closReading, consumption;
    public ReadingData(String pdtName, String keyId) {
        this.pdtName = pdtName;
        this.keyId = keyId;
    }
}

在这里,如果我在recyclerView的起始项中输入值,那么当我将项目向上滚动到列表的底部时,最后一项将具有该值。

error in the list

请忽略图片的质量,因为我们无法上传2MiB的快照。

此处视图在滚动列表时被回收。如何防止将值复制到列表中的其他项目。

Toast也重复了几次。如何制止这一点。

更新: 通过LQ GioanSO question How ListView's recycling mechanism works的建议,我得到了ListView实际上如何回收视图的逻辑。

但我不确定recyclerView是否也一样。

但在我的情况下,我该如何实现这个过程。请别人来帮助我。

2 个答案:

答案 0 :(得分:1)

RecyclerView重用视图,实际上,它仅生成与屏幕上可见的视图一样多的视图。因此可以预期是否会看到为其他行设置的值

该解决方案将设置您要更改为默认视图的视图的所有属性,或者将数据集中应显示的行设置为

因此,放置addTextChangedListener insode ViewHolder构造函数(您可以通过调用getAdapterPosition()来获得位置)以获得更好的性能,并在editText方法的onBindViewHolder内设置{您的数据集

答案 1 :(得分:0)

您的活动代码:

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

适配器类

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]);
}}