I'm having a hard time using spinner to create a checkbox in my project.
The files RegisterBook.java
and BookAdapter.java
return errors. these errors involve getText()
, getSelectedItem()(for condition disallow default option)
and setText(string)
.
Spinner doesn't exist these setText()
and getText()
methods, I don't know which proper methods I should use.
- File
string.xml
<resources>
<string name="app_name">Bookapp</string>
<string-array name="spinnerOptions_book">
<item> Choose the book category </item>
<item> Biography </item>
<item> Education </item>
<item> Literature </item>
<item> HQ, Graphic Novel, Manga </item>
</string-array>
</resources>
- File
activity_register_book.xml
<Spinner
android:id="@+id/spinnerCategory_book"
android:layout_width="match_parent"
android:layout_height="34dp"
android:contentDescription="Checkbox to choose the book category"
android:entries="@array/spinnerOptions_book"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="181dp" />
- File
RegisterBook.java
Spinner spinnerCategory_book;
spinnerCategory_book = findViewById(R.id.spinnerCategory_book);
//error: cannot find symbol method getText()
String category_book = spinnerCategory_book.getText().toString();
//error: cannot find symbol method getSelectedItem()
if (category_book.getSelectedItem().equals("Choose the book category")){
Toast.makeText(RegisterBook.this,"No form fields can be empty",Toast.LENGTH_SHORT).show();
}
- File
BookAdapter.java
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
final Book f_book = books.get(position);
// error: cannot find symbol method setText(String)
holder.spinnerCategory_book.setText(f_book.getCategory_book());
});