Android Spinner using ArrayList<String>

I have been using values of spinner from XML string-arrays so far like:

Spinner karant_sp; 
karant_sp = (Spinner) findViewById(R.id.spinner1); 
ArrayAdapter<CharSequence> karant_adapter = ArrayAdapter.createFromResource(this, R.array.karant_list, android.R.layout.simple_spinner_item);
karant_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
karant_sp.setAdapter(karant_adapter);
karant_sp.setSelection(0);
karant_sp.setOnItemSelectedListener(new select_karant());

Can I use a local java ArrayList-String as the list of items to be displayed in drop down menu?

  • What is the difference between “int” and “long” argument in onItemClick in Android
  • How to Send/receive Data via bluetooth android phone to medical equipment having bluetooth
  • How to show exact number of items in RecyclerView?
  • Custom DatePicker
  • FCM Token Issue in some android device
  • Ubuntu PPA for Android SDK/NDK?
  • Something like:

    Spinner karant_sp; 
    ArrayList<String> return_likes = new ArrayList<String>();
    return_likes.add("Hello");
    return_likes.add("world");
    karant_sp = (Spinner) findViewById(R.id.spinner1); 
    ArrayAdapter<CharSequence> karant_adapter = ArrayAdapter.createFromResource(this, return_likes, android.R.layout.simple_spinner_item);
    karant_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    karant_sp.setAdapter(karant_adapter);
    karant_sp.setSelection(0);
    karant_sp.setOnItemSelectedListener(new select_karant());
    

    I know the above code doesn’t work, but I would like to know something which works using ArrayList-string.

    Related posts:

    How can I monitor the network connection status in Android?
    onDestroy gets called each time the screen goes on
    how to display an activity automatically after 5 seconds
    How do I use InputType=numberDecimal with the &ldquo;phone&rdquo; soft keypad?
    Ionic: How to display ion-item on multiple lines?
    Google Maps Android API v2 creating a new LocationSource
  • Android - How to load HelloWorld app onto my Phone?
  • Can't downgrade database from version `n` to `n-1` on Samsung
  • Call WebView#getDrawingCache() asynchrnously without freezing UI thread
  • Don't collapse Toolbar when RecyclerView fits the screen
  • How Do I Split A String By Line Break?
  • Build Unity project with Jenkins failed
  • One Solution collect form web for “Android Spinner using ArrayList<String>”

    Yes. Just don’t call createFromResource() on your Adapter. Use one of the constructors instead. Something like

    ArrayAdapter<String> karant_adapter = new ArrayAdapter<String>(this,
         android.R.layout.simple_spinner_item, return_likes);
    

    This is just an example you might have to change something else to get it working depending on what isn’t working. But The ArrayAdapter Docs should help.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.