Search code examples
androidandroid-spinner

EditText wont add data to Spinner


I have a fragment that asks the user to enter subjects which will then be saved into a Spinner. The problem is that when I press add, nothing happens. I have a toast message to check if it's added or not and neither shows. I believe I'm doing something wrong because I have a fragment and the code might be different from what I'm used to. I have nothing relating to this in my MainActivity class. I'm a beginner so I don't understand much, any help would be appreciated, thanks.

Code for my Fragment:


public class SubjectFragment extends Fragment {

    Spinner sp;
    RecyclerView recyclerView;
    FloatingActionButton newTaskBtn;
    EditText addSubjectEntry;
    Button addSubBtn, deleteBtn;
    myDatabase myDB;
    ArrayList<String> subjects = new ArrayList<String>();
    ArrayAdapter<String> adapter;


    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        sp = view.findViewById(R.id.addSubjectSpinner);
        addSubBtn = view.findViewById(R.id.addSubBtn);
        addSubjectEntry = view.findViewById(R.id.addSubjectEntry);
        deleteBtn = view.findViewById(R.id.deleteBtn);

        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, subjects);
        sp.setAdapter(adapter);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
                addSubjectEntry.setText(subjects.get(pos));
            }

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

            }
        });
        addSubBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                add();
            }
        });
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                delete();
            }
        });

        return inflater.inflate(R.layout.fragment_message, container, false);

    }

    private void add() {
        String subject = addSubjectEntry.getText().toString();
        if (!subject.isEmpty() && subject.length() < 0) {
            adapter.add(subject);
            adapter.notifyDataSetChanged();
            addSubjectEntry.setText("");
            Toast.makeText(getActivity(), "Added", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getActivity(), "Nothing to add!", Toast.LENGTH_SHORT).show();
        }
    }

    private void delete() {
        int pos = sp.getSelectedItemPosition();
        if (pos > -1) {
            adapter.remove(subjects.get(pos));
            adapter.notifyDataSetChanged();
            Toast.makeText(getActivity(), "Deleted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getActivity(), "Nothing to delete!", Toast.LENGTH_SHORT).show();
        }
    }
}

XML file:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="Add/Delete Subjects"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/addSubjectSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/addSubjectEntry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="100dp"
        android:hint="Subject Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/deleteBtn" />

<!--    <view-->
<!--        android:id="@+id/view"-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="1dp"-->
<!--        android:layout_marginTop="60dp"-->
<!--        app:layout_constraintTop_toBottomOf="@+id/deleteBtn"-->
<!--        tools:layout_editor_absoluteX="0dp" />-->

    <Button
        android:id="@+id/addSubBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:text="@string/add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/addSubjectEntry"
        android:textAllCaps="false"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Subject"
        app:layout_constraintBottom_toTopOf="@+id/addSubjectSpinner"
        app:layout_constraintStart_toStartOf="@+id/addSubjectSpinner" />

    <Button
        android:id="@+id/deleteBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:text="Delete"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/addSubjectSpinner"
        android:textAllCaps="false"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • You have created layout two times. You inflate you first layout, set on click listener to the layout's views and then create another layout, thats the reason why nothing was happened. Just replace last line in the onCreateView method:

        return inflater.inflate(R.layout.fragment_message, container, false);
    

    with

    return view;