RecyclerView在新行中显示在EditText中输入的先前值 [英] RecyclerView shows previous values entered in an EditText in new rows

查看:124
本文介绍了RecyclerView在新行中显示在EditText中输入的先前值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个android应用,其中使用recyclerView,而recyclerView所在的行中包含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的开始项中输入值,那么当我向上滚动项至列表的底部时,最后一项将具有该值.

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

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

Toast也重复了几次.如何阻止这种情况.

更新: 通过 LQ Gioan 的建议,通过SO question 解决方案

RecyclerView重用视图,实际上,它只能生成屏幕上可见的尽可能多的视图.因此可以预期是否会看到为其他行设置的值

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

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

I'm creating an android app, in which I'm using recyclerView and the row of recyclerView is having editText.

This is my ReadingAdapter class

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

This is my ReadingData.java

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

Here, if I enter value in the starting items of the recyclerView then as I scroll up the items to the bottom of the list, the last item will have that value.

Please ignore the quality of image as we can't upload above of 2MiB of snap.

Here the views are recycled as the list is scrolled. How to prevent the copying values to the other item in the list.

And that Toast is also repeated several times. How to stop this.

update: By the suggetion of LQ Gioan through the SO question How ListView's recycling mechanism works , I got the logic how ListView actually works with recycling of views.

But I'm not sure whether the recyclerView also works same.

But here in my case, how can I implement this process. pls someone help me here.

解决方案

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

这篇关于RecyclerView在新行中显示在EditText中输入的先前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆