Skip to content Skip to sidebar Skip to footer

How To Set The Text In To Spinner Using Android

How to set the text into Spinner using Android. Edittext ed1 = findViewById(R.id.pname); spinner spinner = findViewById(R.id.catid); Edit text i will able to set the text but

Solution 1:

The spinner.setSelected() doesn't require a String but a boolean . So try this code to set text to spinner

public void setSpinText(Spinner spinner, String text){
    for(int i= 0; i < spinner.getAdapter().getCount(); i++) {
        if(spin.getAdapter().getItem(i).toString().contains(text)) {
            spinner.setSelection(i);
        }
    }
}

Then you go like this :

Stringt1= i.getStringExtra("product").toString();

 Stringt2= i.getStringExtra("category").toString();

 ed1.setText(t1);
 setSpinText(spinner,t2);

Solution 2:

the setSelected(bool) indicates the selection state of the spinner, it accepts a boolean as parameters and you have tried to put a string, it's so normal to get this error.

You can use spinner.setSelection(int) and enter the position of the item that you want to select. To get your item's position you can use the spinner's adapter by adapter.getPosition("yourValue")

this was an example:

int position = adapter.getPosition("category");
spinner.setSelection(position);

Post a Comment for "How To Set The Text In To Spinner Using Android"