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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:33:41+00:00 2026-06-14T22:33:41+00:00

I have my multiple selection dialog running and popping up with the right values.

  • 0

I have my multiple selection dialog running and popping up with the right values. However I’m struggling to understand how to set and retrieve the values when the save button is clicked.

My code is the following:

   public void addCondition(View view){

        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
        Cursor f = db.rawQuery("select * from assetobservationtypes", null);  
        Log.e("Asset Helper Types:", "Cursor run");
         List<CharSequence> mHelperNames = new ArrayList<CharSequence>();
         final ArrayList<String> mHelperNamesID= new ArrayList<String>();  

        if(f.getCount() > 0) {
            f.moveToFirst();
               while(!f.isAfterLast()) {
                   mHelperNames.add(f.getString(f.getColumnIndex("Observation")));
                   mHelperNamesID.add(f.getString(f.getColumnIndex("AssetObsID")));

                    f.moveToNext();
               }
        }

        f.close();

        final List<Integer> mSelectedItems = new ArrayList<Integer>();

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("My Title")
                    .setMultiChoiceItems(mHelperNames.toArray(new CharSequence[mHelperNames.size()]), null,
                            new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which, boolean isChecked) {
                                    if (isChecked) {

                                        mSelectedItems.add(which);
                                    } else if (mSelectedItems.contains(which)) {

                                        mSelectedItems.remove(Integer
                                                .valueOf(which));
                                    }
                                }
                            })

                   .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {                      
                           txtCondition.setText("set");
                           txtCondition.setTextColor(Color.parseColor("#4c9226"));
                           count++;
                       }
                   })
                   .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.dismiss();
                       }
                   });
            builder.show();
    }

I understand the list is in the array mSelectedItems, however when i tried to do a log of mSelectedItems.get(0) on the onclick of setPositiveButton to test it held anything the app just crashed.

How do I retrieve / store these values? And then pre-populate the list next time

Tom

  • 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-14T22:33:42+00:00Added an answer on June 14, 2026 at 10:33 pm

    I guess that by pre populating the list next time you want to show the checked items in the AlertDialog when the dialog is showed again. In this case make the mSelectedItems a field in your Activity(will initially be an empty list):

    final List<Integer> mSelectedItems = new ArrayList<Integer>();
    

    and your calling method:

    public void addCondition(View view) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
        Cursor f = db.rawQuery("select * from assetobservationtypes", null);  
        Log.e("Asset Helper Types:", "Cursor run");
        List<CharSequence> mHelperNames = new ArrayList<CharSequence>();
        final ArrayList<String> mHelperNamesID= new ArrayList<String>();  
        while(f.moveToNext()) {
            mHelperNames.add(f.getString(f.getColumnIndex("Observation")));
            mHelperNamesID.add(f.getString(f.getColumnIndex("AssetObsID")));
        }
        f.close();
        boolean[] checkedItems = new boolean[mHelperNames.size()];
        for (int  i = 0; i < checkedItems.length; i++) {
            checkedItems[i] = false;
        }
        for (int i = 0; i < mSelectedItems.size(); i++) {
            checkedItems[mSelectedItems.get(i).intValue()] = true;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("My Title").setMultiChoiceItems(mHelperNames.toArray(new CharSequence[mHelperNames.size()]), checkedItems,
                            new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which, boolean isChecked) {
                                    if (isChecked) {
    
                                        mSelectedItems.add(which);
                                    } else if (mSelectedItems.contains(which)) {
    
                                        mSelectedItems.remove(Integer
                                                .valueOf(which));
                                    }
                                }
                            })
    
                   .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {                      
                           txtCondition.setText("set");
                           txtCondition.setTextColor(Color.parseColor("#4c9226"));
                           count++;
                       }
                   })
                   .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.dismiss();
                       }
                   });
            builder.show();
    }
    

    Keep in mind that the mHelperNames list should be the same(you can’t change values in the Cursor)otherwise you risk in having some inconsistency between checked/unchecked items or even exceptions. I hope the code is right, I didn’t tested it.

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

Sidebar

Related Questions

I have a ListBox with multiple selection. And I am doing drag and drop
I have a registration form with common registration fields and two multiple selection Lists
I have a WPF ListBox that I would like to Enable multiple selection in
I want to have a element that displays in the multiple selection display style
I have a selectbox with the multiple attribute set. I'm also using <optgroup> tags
I have simple multiple selection input <input multiple=true type=file name=image_name[] /> Now i would
I have the following scenario. I have a combobox where multiple selection is available.
I have a multiple-selection enabled QListView with an Item delegate (QStyledItemDelegate) I use for
I have datatable with multiple checkbox selection (PF 3.3.1). I've defined column: <p:column selectionMode=multiple/>
We have a one-question checkbox (multiple selection) survey in SharePoint. When selecting Show a

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.