0

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());
    });
Javlo
  • 25
  • 6
  • The `Spinner` class has no such methods (`setText` and `getText`); what are you trying to do? –  Jun 01 '21 at 20:09
  • @Andy i'm trying to correct these methods, I thought that Spinner accepted these methods. – Javlo Jun 01 '21 at 22:08
  • For the "get" I'd guess what you want is `spinnerCategory_book.getSelectedItem().toString()` and the conditional is simply `category_book.equals("..."). –  Jun 01 '21 at 23:26
  • @Andy your comments have been helpful, almost all errors have been corrected. But still the error in the ```holder.spinnerCategory_book.setText(f_book.getCategory_book());``` line still persists. – Javlo Jun 02 '21 at 11:01
  • @Andy from the ```BookAdapter.java``` file. – Javlo Jun 02 '21 at 11:03

0 Answers0