Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing SharedPreferences to store state of checkbox within a listview
    text
    copied!<p>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.</p> <p>My Adapter:</p> <pre><code>public class MobileArrayAdapter extends ArrayAdapter&lt;String&gt; { private final Context context; private final String[] values; private ArrayList&lt;Boolean&gt; itemChecked = new ArrayList&lt;Boolean&gt;(); public MobileArrayAdapter(Context context, String[] values) { super(context, R.layout.list_adapter, values); this.context = context; this.values = values; for (int i = 0; i &lt; 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; } } </code></pre> <p>My main Activity:</p> <pre><code>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(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload