1. Are you ready for the Galaxy S20? Here is everything we know so far!

how to save user input in RecyclerView

Discussion in 'Android Development' started by Artem Polovin, Apr 6, 2020.

  1. Artem Polovin

    Artem Polovin Lurker
    Thread Starter

    Hello everyone. I add “Employee” to the SQLite database. When adding an employee to the database, the user must fill in the fields that relate to the employee, for example, first name, last name, salary, etc. The employee has several attributes such as "skill" and "department" of these attributes the employee may have several, so when the user fills in all the fields with attributes he is invited to add another field, for example, the field "skill" to add another "skill", such a field " skill "you can add as much as you want and I implemented it with RecyclerView. But the problem is this: when the user wants to add another "skill" field, an empty EditText appears and the user fills this field with data, but I don’t know how to save this line, because when you click add skill an empty line is transferred to the adapter, then the user fills this line, but how to save this line to the list that contains the adapter I can not figure out




    This is Adapter class

    public class AdapterAddSkill extends RecyclerView.Adapter<AdapterAddSkill.MyViewHolderAddEmployee> {
    private ArrayList<String> skillsList;
    private Context context;

    public AdapterAddSkill(ArrayList<String> skillsList, Context context) {
    this.skillsList = skillsList;
    this.context = context;
    }

    @NonNull
    @override
    public MyViewHolderAddEmployee onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_skill, parent, false);
    return new MyViewHolderAddEmployee(view);
    }

    @override
    public void onBindViewHolder(@NonNull MyViewHolderAddEmployee holder, final int position) {
    holder.closeCard.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View v) {
    skillsList.remove(position);
    notifyDataSetChanged();
    }
    });
    }

    @override
    public int getItemCount() {
    return skillsList.size();
    }

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

    @override
    public int getItemViewType(int position) {
    return position;
    }



    public static class MyViewHolderAddEmployee extends RecyclerView.ViewHolder {
    private ImageButton closeCard;
    private EditText skillEditText;

    public MyViewHolderAddEmployee(@NonNull View itemView) {
    super(itemView);
    closeCard = itemView.findViewById(R.id.deleteSkillCard);
    skillEditText = itemView.findViewById(R.id.skillText);
    }
    }
    }

    Here is the activity in which the user fills in the data about the employee and adds it to the database

    public class AddEmploy extends AppCompatActivity {
    private EditText nameEditText, lastNameEditText, employeeIdEditText,
    salaryEditText, departmentEditText, skillEditText;
    private ArrayList<String> skillsList = new ArrayList<>();
    private ArrayList<String> departmentsList = new ArrayList<>();
    private AdapterAddSkill adapterAddSkill;
    private RecyclerView skillRecyclerView;
    private AdapterAddDepartment adapterAddDepartment;
    private RecyclerView departmentRecyclerView;


    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_employ);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    nameEditText = findViewById(R.id.name);
    lastNameEditText = findViewById(R.id.lastName);
    employeeIdEditText = findViewById(R.id.employeeId);
    salaryEditText = findViewById(R.id.salary);
    departmentEditText = findViewById(R.id.department);
    skillEditText = findViewById(R.id.skill);
    skillRecyclerView = findViewById(R.id.rv_skill);
    departmentRecyclerView = findViewById(R.id.rv_department);
    adapterAddSkill = new AdapterAddSkill(skillsList, this);
    adapterAddDepartment = new AdapterAddDepartment(departmentsList, this);
    skillRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    departmentRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    skillRecyclerView.setAdapter(adapterAddSkill);
    departmentRecyclerView.setAdapter(adapterAddDepartment);

    }

    public void onClickBtn(View view) {
    switch (view.getId()) {
    case R.id.addDepartmentText:
    departmentsList.add("");
    adapterAddDepartment.notifyDataSetChanged();
    break;
    case R.id.addSkillText:
    skillsList.add("");
    adapterAddSkill.notifyDataSetChanged();
    break;
    case R.id.saveEmployee_info:
    addEmployToDatabase();
    addDepartmentToDatabase();
    addSkillToDatabase();
    }
    }

    @override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
    finish();
    }
    return super.onOptionsItemSelected(item);
    }

    private void addEmployToDatabase() {
    String employeeId = employeeIdEditText.getText().toString();
    String name = nameEditText.getText().toString();
    String lastName = lastNameEditText.getText().toString();
    String salary = salaryEditText.getText().toString();
    MyQuery.addEmployee(new Employ(employeeId, name, lastName, salary));
    }

    private void addSkillToDatabase() {
    String employeeId = employeeIdEditText.getText().toString();
    String skill = skillEditText.getText().toString();
    MyQuery.addSkill(new Skill(employeeId, skill));
    for (int i = 0; i < skillsList.size(); i++) {
    MyQuery.addSkill(new Skill(employeeIdEditText.getText().toString()
    , skillsList.get(i)));
    }
    }

    private void addDepartmentToDatabase() {
    String department = departmentEditText.getText().toString();
    String employeeId = employeeIdEditText.getText().toString();
    MyQuery.addDepartment(new Department(employeeId, department));
    for (int i = 0; i < departmentsList.size(); i++) {
    MyQuery.addDepartment(new Department(employeeIdEditText.getText().toString(),
    departmentsList.get(i)));
    }
    }

    private void showAllEmployees() {
    List<Employ> employs = MyQuery.getAllEmployees();
    for (int i = 0; i < employs.size(); i++) {
    Log.i(".......Employees", employs.get(i).toString());
    }
    }

    private void showAllSkills() {
    List<Skill> skills = MyQuery.getAllSkills();
    for (int i = 0; i < skills.size(); i++) {
    Log.i("......Skills", skills.get(i).toString());
    }
    }

    private void showAllDepartments() {
    List<Department> departments = MyQuery.getAllDepartments();
    for (int i = 0; i < departments.size(); i++) {
    Log.i(".........Departments", departments.get(i).toString());
    }
    }
    }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...