EditText 不会向 Spinner 添加数据
EditText wont add data to Spinner
我有一个片段要求用户输入主题,然后将其保存到 Spinner
中。问题是当我按下添加时,没有任何反应。我有一条 toast 消息来检查它是否已添加,但均未显示。我相信我做错了什么,因为我有一个片段,代码可能与我习惯的不同。我的 MainActivity
class 中没有与此相关的内容。我是初学者,所以我不太了解,任何帮助将不胜感激,谢谢。
我的片段代码:
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 文件:
<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;
我有一个片段要求用户输入主题,然后将其保存到 Spinner
中。问题是当我按下添加时,没有任何反应。我有一条 toast 消息来检查它是否已添加,但均未显示。我相信我做错了什么,因为我有一个片段,代码可能与我习惯的不同。我的 MainActivity
class 中没有与此相关的内容。我是初学者,所以我不太了解,任何帮助将不胜感激,谢谢。
我的片段代码:
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 文件:
<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;