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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:29:38+00:00 2026-06-03T01:29:38+00:00

How would I use shared preferences to store the state of my checkbox for

  • 0

How would I use shared preferences to store the state of my checkbox for the next time the app is opened? I’m using a custom adapter so am guessing it has to be placed inside that but I’m not quite sure.

My Adapter:

public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();


    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_adapter, values);
        this.context = context;
        this.values = values;


        for (int i = 0; i < this.getCount(); i++) {
            itemChecked.add(i, false);
        }
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.list_adapter,
                    parent, false);
        }
           // in your code you search for the CheckBox with the id checkBox1 2 times so I assumed that you are referring to the same view.
        CheckBox cBox = (CheckBox) rowView.findViewById(R.id.checkBox1);
        cBox.setTextColor(0xFFFFFFFF);
        cBox.setText(values[position]);       
        cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (isChecked) {
                    itemChecked.set(position, true);
                    // do some operations here
                } else {
                    itemChecked.set(position, false);
                    // do some operations here
                }
            }
        });
        cBox.setChecked(itemChecked.get(position));
        return rowView;
    }

}

My main Activity:

public class TheKevinAndEricaBoxActivity extends Activity {
/** Called when the activity is first created. */
private String[] myString;
private String list;
private String[] myString2;
private String list2;
private static final Random rgenerator = new Random();
private static final Random rgenerator2 = new Random();
MediaPlayer mp;
final Context mContext = this;
final Context context = this;
private Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Resources res = getResources();
    addListenerOnButton();




    myString = res.getStringArray(R.array.myArray);

    list = myString[rgenerator.nextInt(myString.length)];

    myString2 = res.getStringArray(R.array.myArray2);

    list2 = myString2[rgenerator.nextInt(myString2.length)];
}



    public void addListenerOnButton() {

        final Context context2 = this;

        ImageButton ibg = (ImageButton) findViewById(R.id.buttongallery);

        ibg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context2, App2Activity.class);
                startActivityForResult(intent, 0);   

            }

        });


    ImageButton ib = (ImageButton) findViewById(R.id.imagebutton1);
    ib.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View erica) {
            AlertDialog.Builder b = new AlertDialog.Builder(
                    TheKevinAndEricaBoxActivity.this);
            b.setMessage(myString[rgenerator.nextInt(myString.length)]);
            b.setTitle(R.string.title1);
            b.setIcon(R.drawable.menuiconerica);
            b.setPositiveButton("Back",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.cancel();
                        }
                    });
            Dialog d = b.create();
            d.show();
        }
    });

    ImageButton ib2 = (ImageButton) findViewById(R.id.imagebutton2);
    ib2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View kevin) {
            AlertDialog.Builder b = new AlertDialog.Builder(
                    TheKevinAndEricaBoxActivity.this);
            b.setMessage(myString2[rgenerator2.nextInt(myString2.length)]);
            b.setTitle(R.string.title2);
            b.setIcon(R.drawable.menuiconkevin);
            b.setPositiveButton("Back",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.cancel();
                        }
                    });
            Dialog d = b.create();
            d.show();

        }
    });


    ImageButton Ib3 = (ImageButton) findViewById(R.id.imagebutton3);
    Ib3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View lemonclick) {
            mp = MediaPlayer.create(getApplicationContext(),R.raw.lemonspeech);
            mp.start();


        }
    });
    button = (Button) findViewById(R.id.button01);

    // add button listener
    button.setOnClickListener(new OnClickListener() {

      public void onClick(View arg0) {

        // custom dialog
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.list);
        dialog.setTitle("The List");


        // set the custom dialog components - text, image and button
        //TextView text = (TextView) dialog.findViewById(R.id.TextView01);
        //text.setText("Did you not read the button? :P i'm not finshed on this yet XD");


        ListView listView = (ListView) findViewById(R.id.myList);
        String[] values = new String[] { "value1", "value2", };


        MobileArrayAdapter mAdapter = new MobileArrayAdapter(getBaseContext(), values);
        ListView mListView = (ListView) dialog.findViewById(R.id.myList);
        mListView.setAdapter(mAdapter);


        Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
      }

    });
    }
}
  • 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-03T01:29:40+00:00Added an answer on June 3, 2026 at 1:29 am

    In the OnCLickListener for your Button add this:

    //...
    // custom dialog
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.list);
    dialog.setTitle("The List");
    
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefsEditor = prefs.edit();
    String currentlyStored = prefs.getString("checked_list", null);
    int[] savedStatus = null;
    if (currentlyStored != null) {
        String[] tmp = currentlyStored.split(",");
        savedStatus = new int[tmp.length];
        for (int i = 0; i < tmp.length; i++) {
            savedStatus[i] = Integer.parseInt(tmp[i]);
        }
    }
    adapter = new MobileArrayAdapter(this, soundnames, savedStatus);
    ListView mListView = (ListView) dialog.findViewById(R.id.myList);
    mListView.setAdapter(mAdapter);
    //...
    

    where:

    private SharedPreferences prefs;
    private SharedPreferences.Editor prefsEditor;
    private MobileArrayAdapter adapter;
    

    are fields in your class with the ListView(the adapter field will hold your adapter object that you set on the list).

    Modify the constructor of your custom adapter like this:

    public MobileArrayAdapter(Context context, String[] values,
                    int[] oldStatus) {
                super(context, R.layout.adapters_simpleplay_row, values);
                this.context = context;
                this.values = values;
    
                // make every CheckBox unchecked and then loop through oldStatus(if
                // not null)
                for (int i = 0; i < this.getCount(); i++) {
                    itemChecked.add(i, false);
                }
                if (oldStatus != null) {
                    for (int j = 0; j < oldStatus.length; j++) {
                        itemChecked.set(oldStatus[j], true);
                    }
                }
            }
    

    Also add the following method in your custom adapter MobileArrayAdapter:

    public ArrayList<Boolean> getCheckedStatus() {
                return itemChecked;
    }
    

    Last in the listener for your dialogButton add this:

    dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                                String toStore = "";
                ArrayList<Boolean> status = adapter.getCheckedStatus();
                for (int i = 0; i < status.size(); i++) {
                    if (status.get(i)) {
                        toStore += i + ",";
                    }
                }
                prefsEditor.putString("checked_list", toStore.equals("") ? null
                        : toStore.substring(0, toStore.length() - 1));
                prefsEditor.commit();
                    dialog.dismiss();
                }
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the sharer.php popup to open the share dialog, but would
I would use a multi dimensional gaussian modell for regression. Rasmussen has a book
I would use Jmagick into an Android Project. Is it possible? I have to
I thought I would use a stored routine to clean up some of my
In .NET I would use System.Diagnostics.Trace... What would I use in C or C++
I just would like to know what format and codec would use the least
What are the main reasons someone would use HTML's tbody in a table? Is
I'm having trouble understanding why I would use a context.xml file to declare a
I am writing a Facebook application that would use a Postgres DB along with
Can I use generateDS.py in python in a similar way that I would use

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.