Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You question didn't say anything about storing the data after the application has closed. To store the checked <code>CheckBoxes</code> you could use a database or a simple file. Bellow is an example of storing the <code>CheckBox</code> state in a database:</p> <pre><code>public class SimplePlay extends ListActivity { private String[] soundnames; private Helper mHelper = new Helper(this, "position_status.db", null, 1); private SQLiteDatabase statusDb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // this is to simulate data soundnames = new String[40]; for (int i = 0; i &lt; 40; i++) { soundnames[i] = "Sound " + i; } // Retrieve the list of position that are checked(if any) from the // database statusDb = mHelper.getWritableDatabase(); Cursor statusCursor = statusDb.rawQuery("SELECT * FROM status", null); int[] savedStatus = null; if ((statusCursor != null) &amp; (statusCursor.moveToFirst())) { savedStatus = new int[statusCursor.getCount()]; int i = 0; do { savedStatus[i] = statusCursor.getInt(0); i++; } while (statusCursor.moveToNext()); } // if the cursor is null or empty we just pass the null savedStatus to // the adapter constructor and let it handle(setting all the CheckBoxes // to unchecked) setListAdapter(new MobileArrayAdapter(this, soundnames, savedStatus)); } 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, 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 &lt; this.getCount(); i++) { itemChecked.add(i, false); } if (oldStatus != null) { for (int j = 0; j &lt; oldStatus.length; j++) { itemChecked.set(oldStatus[j], true); } } } @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.adapters_simpleplay_row, parent, false); } CheckBox cBox = (CheckBox) rowView.findViewById(R.id.checkBox1); cBox.setTextColor(0xFFFFFFFF); cBox.setText(values[position]); cBox.setTag(new Integer(position)); cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int realPosition = (Integer) buttonView.getTag(); if (isChecked) { itemChecked.set(realPosition, true); // update the database to store the new checked item: ContentValues cv = new ContentValues(); cv.put("list_position", realPosition); statusDb.insert("status", null, cv); } else { itemChecked.set(realPosition, false); // delete this position from the database because it was // unchecked statusDb.delete("status", "list_position = " + realPosition, null); } } }); cBox.setChecked(itemChecked.get(position)); return rowView; } } //for working with the database private class Helper extends SQLiteOpenHelper { public Helper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { // the list_position will hold the position from the list that are // currently checked String sql = "CREATE TABLE status (list_position INTEGER);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Just for interface } } } </code></pre> <p>After a quick test, the code works. It is just an example.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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