Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:17:27+00:00 2026-06-10T07:17:27+00:00

I have created a Spinner that is populated with an ArrayList . I want

  • 0

I have created a Spinner that is populated with an ArrayList. I want to dynamically add values to the ArrayList, so the the Spinner populates dynamically. However, when I try to add values to my ArrayList, I get a NullPointerException.

What am I missing? Do I have to reset the adapter before amending the ArrayList?

Here is my code:

My spinner, arraylist, and adapter:

deleteselection = (Spinner)view.findViewById(R.id.deletespinner);
portfoliosdelete = new ArrayList<String>();
portfoliosdelete.add("Select Portfolio");
adapterdeletetype = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,portfoliosdelete){

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        View v = null;

        // If this is the initial dummy entry, make it hidden
        if (position == 0) {
            TextView tv = new TextView(getContext());
            tv.setHeight(0);
            tv.setVisibility(View.GONE);
            v = tv;
        }
        else {
            // Pass convertView as null to prevent reuse of special case views
            v = super.getDropDownView(position, null, parent);
        }

        // Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling
        parent.setVerticalScrollBarEnabled(false);
        return v;
    }
};
adapterdeletetype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
deleteselection.setAdapter(adapterdeletetype);

My code to dynamically update spinner:

 else if(users.contains(usernull)){
    pn1 = enterportfolioname.getText().toString();
    user1 = new PortfolioRecord(pn1, "someemail@gmail.com");
    users.remove(usernull);
    users.add(user1);
    portfoliosdelete.add(pn1); // <-- This causes a null pointer exception
    adapterdeletetype.notifyDataSetChanged();
    portfoliolist.invalidateViews();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:17:29+00:00Added an answer on June 10, 2026 at 7:17 am

    Use the code below. This code will add a new item when the user selects and add a new item from the spinner.

    Code sample:

    layout main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="10" >
    
        <Spinner
            android:id="@+id/cmbNames"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    layout spinner_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    Activity class:

    public class MainActivity extends Activity {
        private static final String NAME = "name";
        private static final String ADD_NEW_ITEM = "Add New Item";
    
        private SimpleAdapter adapter;
        private Spinner cmbNames;
        private List<HashMap<String, String>> lstNames;
        private int counter;
    
        private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {
    
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                HashMap<String, String> map = lstNames.get(arg2);
                String name = map.get(NAME);
                if (name.equalsIgnoreCase(ADD_NEW_ITEM)) {
                    lstNames.remove(map);
                    counter++;
                    addNewName(String.valueOf(counter));
                    addNewName(ADD_NEW_ITEM);
                    adapter.notifyDataSetChanged();
                }
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            populateList();
    
            cmbNames = (Spinner) findViewById(R.id.cmbNames);
            adapter = new SimpleAdapter(this, lstNames, R.layout.spinner_item,
                    new String[] { NAME }, new int[] { R.id.tvName });
            cmbNames.setAdapter(adapter);
            cmbNames.setOnItemSelectedListener(itemSelectedListener);
        }
    
        private void populateList() {
            lstNames = new ArrayList<HashMap<String, String>>();
    
            addNewName("abc");
            addNewName("pqr");
            addNewName("xyz");
            addNewName(ADD_NEW_ITEM);
        }
    
        private void addNewName(String name) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(NAME, name);
            lstNames.add(map);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application, which has a Spinner that I want populated with some
I have created a drop down in my android application using a spinner. However
I want to add a spinner to my android application that looks like that
I have a spinner that is created using the resource file statically. now i
I have created the following class that extends JSpinner to iterate over dd/mm/yyy values.
another question of mine. I have created a screen that consists of a Spinner
I have created an application for ContactsContract... I have created a spinner which brings
I created an app and have been testing it against targetSdk 14. The spinner
I have created some JQuery that will expand a div 'popup' on hover and
I have created an android application that calls (using kSOAP library) a SOAP based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.