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 8860899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:22:05+00:00 2026-06-14T15:22:05+00:00

I have a listview (multi choice). It works perfect. I used a custom adapter

  • 0

I have a listview (multi choice).
It works perfect. I used a custom adapter for it. There is a text in each row and a checkbox.
Marked items will be shown as a result in a textview.

I want to add a spinner inside listview rows. I get null point exception error.
Thanks in advance.

public class MainActivity extends Activity {

ListView myListView;
Button getResult;
Spinner spinner;

private ArrayList<String> dayOfWeekList = new ArrayList<String>();

private void initDayOfWeekList(){
    dayOfWeekList.add("Sunday");
    dayOfWeekList.add("Monday");
    dayOfWeekList.add("Tuesday");
    dayOfWeekList.add("Wednesday");
    dayOfWeekList.add("Thursday");
    dayOfWeekList.add("Friday");
    dayOfWeekList.add("Saturday");

}

String[] androidBooks = 
    {
    "Hello",
    "Professional",
    "Unlocking"
    };

MyArrayAdapter myArrayAdapter;
ArrayAdapter<String> spinnerAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initDayOfWeekList();
    setContentView(R.layout.activity_main);
    spinnerAdapter = new ArrayAdapter<String> (this, 
            android.R.layout.simple_spinner_item, androidBooks);




    myListView = (ListView)findViewById(R.id.list);

    myArrayAdapter = new MyArrayAdapter(
            this,
            R.layout.row,
            android.R.id.text1,
            dayOfWeekList
            );

    myListView.setAdapter(myArrayAdapter);
    myListView.setOnItemClickListener(myOnItemClickListener);

    getResult = (Button)findViewById(R.id.getresult);
    getResult.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            String result = "";

            /*
            //getCheckedItemPositions
            List<Integer> resultList = myArrayAdapter.getCheckedItemPositions();
            for(int i = 0; i < resultList.size(); i++){
                result += String.valueOf(resultList.get(i)) + " ";
            }
            */

            //getCheckedItems
            List<String> resultList = myArrayAdapter.getCheckedItems();
            for(int i = 0; i < resultList.size(); i++){
                result += String.valueOf(resultList.get(i)) + "\n";
            }

            myArrayAdapter.getCheckedItemPositions().toString();
            Toast.makeText(
                    getApplicationContext(), 
                    result, 
                    Toast.LENGTH_LONG).show();
        }});

}

OnItemClickListener myOnItemClickListener
= new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        myArrayAdapter.toggleChecked(position);

    }};

private class MyArrayAdapter extends ArrayAdapter<String>{

    private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();

    public MyArrayAdapter(Context context, int resource,
            int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);

        for(int i = 0; i < objects.size(); i++){
            myChecked.put(i, false);
        }
    }

    public void toggleChecked(int position){
        if(myChecked.get(position)){
            myChecked.put(position, false);
        }else{
            myChecked.put(position, true);
        }

        notifyDataSetChanged();
    }

    public List<Integer> getCheckedItemPositions(){
        List<Integer> checkedItemPositions = new ArrayList<Integer>();

        for(int i = 0; i < myChecked.size(); i++){
            if (myChecked.get(i)){
                (checkedItemPositions).add(i);
            }
        }

        return checkedItemPositions;
    }

    public List<String> getCheckedItems(){
        List<String> checkedItems = new ArrayList<String>();

        for(int i = 0; i < myChecked.size(); i++){
            if (myChecked.get(i)){
                (checkedItems).add(dayOfWeekList.get(i));
            }
        }

        return checkedItems;
    }

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

        spinner = (Spinner) row.findViewById(R.id.planets_spinner);
        spinner.setAdapter(spinnerAdapter);

        if(row==null){
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.row, parent, false);  
        }

        CheckedTextView checkedTextView =    (CheckedTextView)row.findViewById(R.id.text1);
        checkedTextView.setText(dayOfWeekList.get(position));

        Boolean checked = myChecked.get(position);
        if (checked != null) {
            checkedTextView.setChecked(checked);
        }

        return row;
    }

}

}

  • 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-14T15:22:06+00:00Added an answer on June 14, 2026 at 3:22 pm

    You have your code out of order in getView(..)

    You are trying to retrieve your spinner before you inflate your view.. If convertView is null (which it will be the first time through your list) your spinner will not be found.. you can’t call findViewById on something that’s null.. Change the order of your operations to the following

     @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
    
            if(row==null){
                LayoutInflater inflater=getLayoutInflater();
                row=inflater.inflate(R.layout.row, parent, false);  
            }
    
            spinner = (Spinner) row.findViewById(R.id.planets_spinner);
            spinner.setAdapter(spinnerAdapter);
            CheckedTextView checkedTextView =    (CheckedTextView)row.findViewById(R.id.text1);
            checkedTextView.setText(dayOfWeekList.get(position));
    
            Boolean checked = myChecked.get(position);
            if (checked != null) {
                checkedTextView.setChecked(checked);
            }
    
            return row;
        }
    

    Notice how i moved retrieving the spinner to AFTER checking the view for null and inflating if needed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have listview with custom adapter. In each row i have 2 textviews and
I have a listView multichoice, Each row of list have a checkbox. And I
Hello I have a multi choice listview. I would like to add more than
I have ListView. Every row has TextView with some text. And also by default
i have listview , and in each row needs to be an hoeizontal scrollview
I have ListView. I would like to fill each row of ListView with a
I have ListView with custom Adapter which supplies View to ListView in this way:
I have ListView which as one TextView in each Row. and im discovering some
I have listview control.There is an option to remove selected items.After the user removes
I have a ListView and each item have a TextView. I would like add

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.