1

Имеется RecyclerView элементов Section, необходимо при нажатии определенной кнопки введенные данные в EditText - flow, zStart, length присваивались переменным элемента Section из List Sections

Элемент Section

public class Section {
public double getFlow() {
    return flow;
}

public void setFlow(double flow) {
    this.flow = flow;
}

public double getZStart() {
    return zStart;
}

public void setZStart(double zStart) {
    this.zStart = zStart;
}

private double flow;
private double zStart;
private double length;

public Section(double flow, double zStart, double length) {
    this.flow = flow;
    this.zStart = zStart;
    this.length = length;
}

public double getLength() {
    return length;
}

public void setLength(double length) {
    this.length = length;
}

Адаптер

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private final LayoutInflater inflater;
final List<Section> sections;
private Section section;
private final OnDeleteClickListener onDeleteClickListener;

interface OnDeleteClickListener {
    void onDeleteClick(Section section, int position);
}

public ListAdapter(Context context, List<Section> sections, OnDeleteClickListener onDeleteClickListener) {
    this.inflater = LayoutInflater.from(context);
    this.sections = sections;
    this.onDeleteClickListener = onDeleteClickListener;
}


@Override
public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.section, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ListAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
   section= sections.get(position);
    holder.nameSection.setText("Участок #" + (position + 2));
    holder.deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onDeleteClickListener.onDeleteClick(section, position);
        }
    });
}
public void setData(ListAdapter.ViewHolder holder){
    section.setZStart(Double.parseDouble(String.valueOf(holder.zStartSection.getText())));
}
@Override
public int getItemCount() {
    return sections.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    final TextView nameSection;
    final TextView zStartSectionText, flowSectionText;
    final CheckBox checkBoxSection;
    final EditText zStartSection, flowSection;
    final ImageButton deleteButton;
    final EditText lengthSection;

    ViewHolder(View view) {
        super(view);
        nameSection = view.findViewById(R.id.nameSection);
        zStartSectionText = view.findViewById(R.id.zStartSectionText);
        flowSectionText = view.findViewById(R.id.flowSectionText);
        checkBoxSection = view.findViewById(R.id.checkBoxSectionFlow);
        zStartSection = view.findViewById(R.id.zStartSectionInput);
        deleteButton = view.findViewById(R.id.deleteButton);
        flowSection = view.findViewById(R.id.flowSection);
        lengthSection = view.findViewById(R.id.lengthSectionInput);
        flowSection.getLayoutParams().height = 0;
        flowSectionText.getLayoutParams().height = 0;
        flowSectionText.requestLayout();
        flowSection.requestLayout();
        checkBoxSection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    flowSection.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                    flowSectionText.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                    flowSection.setOnFocusChangeListener(new PressFindHstart.FocusChange(flowSection,"Расход на участке"));
                    flowSection.addTextChangedListener(new TextWatcher() {
                        @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) {
                     
                        }
                    });
                } else {
                    flowSection.getLayoutParams().height = 0;
                    flowSectionText.getLayoutParams().height = 0;
                    flowSection.setOnFocusChangeListener(null);
                    flowSection.setText("");
                }
                flowSectionText.requestLayout();
                flowSection.requestLayout();
            }
        });
        zStartSection.setOnFocusChangeListener(new PressFindHstart.FocusChange(zStartSection, "Отметка начальной точки участка"));
        lengthSection.setOnFocusChangeListener(new PressFindHstart.FocusChange(lengthSection, "Длина участка"));

    }
}

В коде Main

    ArrayList<Section> sections = new ArrayList<>()
    recyclerView = findViewById(R.id.listViewMode);
    recyclerView.getLayoutParams().height = 0;
    addingButton = findViewById(R.id.addingButton);
    View separator = findViewById(R.id.separator);
    onDeleteClickListener = new ListAdapter.OnDeleteClickListener() {
        @Override
        public void onDeleteClick(Section section, int position) {
            sections.remove(position);
            recyclerView.requestLayout();
            if (sections.size() == 0) {
                isMultiSections = false;
                separator.setVisibility(View.INVISIBLE);
            }
        }
    };
    listAdapter = new ListAdapter(this, sections, onDeleteClickListener);
    recyclerView.setAdapter(listAdapter);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (sections.size() < 9) {
                isMultiSections = true;
                recyclerView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                recyclerView.requestLayout();
                sections.add(new Section(0, 0, 0));
                recyclerView.requestLayout();
                if (separator.getVisibility() == View.INVISIBLE)
                    separator.setVisibility(View.VISIBLE);
            } else {
                Toast.makeText(getApplicationContext(), "Достигнуто максимальное количество участков", Toast.LENGTH_LONG).show();
            }
        }
    };

1 ответ 1

0

Нашел способ, дополнил код адаптера

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private final LayoutInflater inflater;
final List<Section> sections;
private Section section;
private final OnDeleteClickListener onDeleteClickListener;

interface OnDeleteClickListener {
    void onDeleteClick(Section section, int position);
}

public ListAdapter(Context context, List<Section> sections, OnDeleteClickListener onDeleteClickListener) {
    this.inflater = LayoutInflater.from(context);
    this.sections = sections;
    this.onDeleteClickListener = onDeleteClickListener;
}


@Override
public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.section, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ListAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
    section = sections.get(position);
    holder.nameSection.setText("Участок #" + (position + 2));
    **holder.deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onDeleteClickListener.onDeleteClick(section, position);
        }
    });
    holder.flowSection.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0 || !s.equals("0"))
                sections.get(position).setFlow(Double.parseDouble(s.toString()));
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}**

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

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.