如何解决EditText 不会向 Spinner 添加数据
我有一个片段,要求用户输入主题,然后将其保存到 Spinner
中。问题是当我按添加时,什么也没有发生。我有一条 toast 消息来检查它是否已添加,但都没有显示。我相信我做错了什么,因为我有一个片段,代码可能与我习惯的不同。我的 MainActivity
课上没有与此相关的任何内容。我是初学者,所以我不太了解,任何帮助将不胜感激,谢谢。
我的片段代码:
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,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","Nothing to delete!",Toast.LENGTH_SHORT).show();
}
}
}
XML 文件:
<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>
解决方法
您已经创建了两次布局。您将第一个布局膨胀,将单击侦听器设置为布局的视图,然后创建另一个布局,这就是什么都没发生的原因。只需替换 onCreateView 方法中的最后一行:
return inflater.inflate(R.layout.fragment_message,container,false);
与
return view;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。