1

I have a spinner in which I am currently showing static data, how can i show dynamic data (for ex : from string array) or something like to adapter so that when i receive dynamic value from server, i can directly feed that to spinner

Below is my code :

if(value.contains("Extra")){
                    spinner.setVisibility(View.VISIBLE);
                    image.setVisibility(View.GONE);
                    triangleimage.setVisibility(View.VISIBLE);
                    spinner.setOnItemSelectedListener(Adapter.this);

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item) {

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {

                            View v = super.getView(position, convertView, parent);
                            if (position == getCount()) {
                                ((TextView)v.findViewById(android.R.id.text1)).setText("");
                                ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
                            }

                            return v;
                        }       

                        @Override
                        public int getCount() {
                            return super.getCount()-1;            // you don't display last item. It is used as hint.
                        }

                    };

                    adapter.setDropDownViewResource(R.layout.custom_spinner_list);
                    adapter.add("One");
                    adapter.add("Two");
                    adapter.add("Three");
                    adapter.add("Four");
                    adapter.add("Five");
                    adapter.add("Six");
                    adapter.add("");                              //Add empty string in db option at last


                    spinner.setAdapter(adapter);
                    spinner.setSelection(adapter.getCount());     //set the hint the default selection so it appears on launch.
                    spinner.setOnItemSelectedListener(this);

                    notifyDataSetChanged();                       

                }
user45678
  • 1,504
  • 6
  • 29
  • 58

3 Answers3

3

There is a constructor in ArrayAdapter to pass array list as parameter. See the sample below.

    // Assume this list is from server 
     List<String> your_array_list = new ArrayList<String>();
     your_array_list.add("Test");
     your_array_list.add("Working");

     // This is the array adapter, it takes the context of the activity as a  
     // first parameter, the type of list view as a second parameter and your  
     // array as a third parameter. 
     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             your_array_list );

     yourSpinner.setAdapter(arrayAdapter); 

This should work.

vinothp
  • 9,939
  • 19
  • 61
  • 103
  • Thanks @Vino. That helped. Can you tell me how can i set "emptyvalue" as default value in same spinner, i use to do that earlier in code i posted in question, but with this even if i set first value as " ", it won't set default value in spinner as empty. – user45678 Sep 15 '15 at 11:05
0

For Dynamic Data To Spinner

First Store All data in a arraylist or array and then pass that to adapter of spinner it's work ...

You can data to array

array_name.add(your_data);
Maulik Santoki
  • 532
  • 4
  • 14
0

Create a listener for dynamic data binding to your spinner. Example 1. Example 2

You can use AsyncTask for retrieving data from server(or use your suitable method).

Community
  • 1
  • 1