J
J
justxz2020-02-17 15:58:33
Android
justxz, 2020-02-17 15:58:33

How to automatically add EditText to RecyclerView?

Description:

I want to add a new EditText to a RecyclerView when the user has entered something in an already existing EditText. But already from the start of the application, 4 EditText elements are created, after you enter something into one of them, several more elements are added.

How I did it:

In onCreate I create a list of strings and add the string ""(empty) to it, pass this list to the adapter. In the adapter I created a TextWatcher, in afterTextChanged I check if the last element of the list is empty, if not, I add "" (an empty string).

This is how the application looks after launch (4 editTexts immediately appear): Activation
Xgyyh.png

code:

public class AddElement extends AppCompatActivity {

ArrayList<String> subtasks;
SubtasksAdapter adapter;
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_element);

    subtasks = new ArrayList<>();
    subtasks.add("");

    recyclerView = findViewById(R.id.recyclerView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    adapter = new SubtasksAdapter(subtasks);
    recyclerView.setAdapter(adapter);

}


Adapter code:
public class SubtasksAdapter extends RecyclerView.Adapter<SubtasksAdapter.SubtasksViewHolder> {

ArrayList<String> elements;

public SubtasksAdapter(ArrayList<String> elements) {
    this.elements = elements;
}

@NonNull
@Override
public SubtasksViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    Context context = parent.getContext();
    int LayoutIdForListItem = R.layout.example_of_item3;
    LayoutInflater layoutInflater = LayoutInflater.from(context);

    View view = layoutInflater.inflate(LayoutIdForListItem, parent, false);

    return new SubtasksViewHolder(view, new CustomEditTextListener());
}

@Override
public void onBindViewHolder(@NonNull SubtasksViewHolder holder, int position) {
    holder.bind(position);

}

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

class SubtasksViewHolder extends RecyclerView.ViewHolder {

    CheckBox checkBox;
    EditText editText;
    CustomEditTextListener customEditTextListener;

    public SubtasksViewHolder(@NonNull View itemView, CustomEditTextListener customEditTextListener) {
        super(itemView);

        this.customEditTextListener = customEditTextListener;

        checkBox = itemView.findViewById(R.id.checkBox);
        editText = itemView.findViewById(R.id.editText);

        editText.addTextChangedListener(customEditTextListener);

    }

    void bind(int position) {

        editText.setHint("Добавьте шаг");

        customEditTextListener.updatePosition(getAdapterPosition());
        editText.setText(elements.get(position));

    }
}

class CustomEditTextListener implements TextWatcher {

    int position;

    public void updatePosition(int position) {
        this.position = position;
    }

    @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) {

        elements.set(position, s.toString());
        String tempString2 = "";
        String tempString1 = elements.get(elements.size() - 1);
        if (tempString1 != tempString2) {
            elements.add("");
        }

    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Yudakov, 2020-02-17
@justxz

In Java, some things are done through w. For example, strings are not compared like this:

if (tempString1 != tempString2) {
    elements.add("");
}

but like this:
if (!tempString1.equals(tempString2)) {
    elements.add("");
}

Better switch to Kotlin.

A
Alex Avaj, 2020-02-19
@sandroisu

adapter.notifyDataSetChanged(); will not help?
if the question is different, then you need to describe the task in a more understandable way, because as I understood from the question, this is all that is needed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question