Spinner routine

I want to fill Spinner with elements that have body text and additional text, just like Android Studio shows when building a view on the Designer tab.

example

So far I have been able to fill it only with the main text.

I am doing this with code. Using a simple adapter.

I tried the following, but without success, it just gives me the same result (only the main text):

Spinner spinner = (Spinner) findViewById(R.id.mySpinner); List<Map<String, String>> itens = new ArrayList<>(); Map<String, String> item = new HashMap<>(2); item.put("text", "MAIN TEXT"); item.put("subText", "SUB TEXT"); itens.add(item); SimpleAdapter adapter = new SimpleAdapter(spinner.getContext(), itens, android.R.layout.simple_spinner_dropdown_item, new String[]{"text", "subText"}, new int[]{android.R.id.text1, android.R.id.text2} ); // i am not sure what this does adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); 
+6
source share
2 answers

I had the same problem and I used the OP code as the basis for creating this solution:

 final Spinner spinner = (Spinner)fragmentView.findViewById(R.id.spinner); List<Map<String, String>> items = new ArrayList<Map<String, String>>(); Map<String, String> item0 = new HashMap<String, String>(2); item0.put("text", "Browse aisles..."); item0.put("subText", "(Upgrade required)"); items.add(item0); Map<String, String> item1 = new HashMap<String, String>(2); item1.put("text", "Option 1"); item1.put("subText", "(sub text 1)"); items.add(item1); Map<String, String> item2 = new HashMap<String, String>(2); item2.put("text", "Option 2"); item2.put("subText", "(sub text 2)"); items.add(item2); SimpleAdapter adapter = new SimpleAdapter(getActivity(), items, android.R.layout.simple_spinner_item, // This is the layout that will be used for the standard/static part of the spinner. (You can use android.R.layout.simple_list_item_2 if you want the subText to also be shown here.) new String[] {"text", "subText"}, new int[] {android.R.id.text1, android.R.id.text2} ); // This sets the layout that will be used when the dropdown views are shown. I'm using android.R.layout.simple_list_item_2 so the subtext will also be shown. adapter.setDropDownViewResource(android.R.layout.simple_list_item_2); spinner.setAdapter(adapter); 

You can also replace android.R.layout.simple_spinner_item and / or android.R.layout.simple_list_item_2 with your own views (which are usually located in the layout folder).

This is a much better solution than PhoneGap !: D

+8
source

You will need to create a custom ArrayAdapter that creates a custom view for the Spinner drop-down list. This link gives a good example: How to customize the Spinner drop-down list

+1
source

Source: https://habr.com/ru/post/979752/


All Articles