Way to set arraylist to spinner

10,922

Solution 1

Please use Context of activity as the first parameter of the ArrayAdapter, you can use

ActivityName.this instead of this, where the ActivityName is the name of the activity class. It seems you are running this code, in some Runnable, or Thread class, so right now, this is the instance of a Runnable object.

Solution 2

Use the following -

Spinner spinnerCategory = (Spinner)findViewById(R.id.spinnercategory);
                ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, categoryList);

Solution 3

Use customized adapter and implement it according to your data. It is just a sample not working code.

 Spinner spinnerCategory = (Spinner)findViewById(R.id.spinnercategory);
          spinnerCategory.setAdapter( new SpinnerAdapter() {

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                // TODO Auto-generated method stub

            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public int getViewTypeCount() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public int getItemViewType(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public Object getItem(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                return null;
            }
        });
Share:
10,922

Related videos on Youtube

Korhan
Author by

Korhan

Updated on October 14, 2022

Comments

  • Korhan
    Korhan over 1 year

    Below is my code. But it shows it is not possible. Can someone please suggest me how to set the arraylist to the spinner rather than setting simple array to the spinner.Below is my code.

    ArrayList<String> categoryList = new ArrayList<String>();
    

    //Here I have code to set string values to arraylist

    //Below is the code where I am attempting to set the arraylist but it says "The constructor ArrayAdapter(new Runnable(){}, int, ArrayList) is undefined"

    Spinner spinnerCategory = (Spinner)findViewById(R.id.spinnercategory);
                        ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoryList);