Note that there are some explanatory texts on larger screens.

plurals
  1. POCheckBoxes in ListView disappear when the screen rotates
    primarykey
    data
    text
    <p>What my application <strong>first</strong> does is it loads <code>ListView</code> whose items have <strong>invisible <code>CheckBoxes</code> by setting its visibility <code>View.Gone</code></strong>. When the user tabs a menu button then it will turn on and off the <code>CheckBox</code> visibility and some other layouts. Below is the code, I removed some unnecessary parts:</p> <pre><code>private void editmodeSwitch(boolean flag){ // get topbar, bottombar, and bottombar2 LinearLayout topbar = (LinearLayout) findViewById(R.id.task_topbar_linearLayout); LinearLayout bottombar = (LinearLayout) findViewById(R.id.task_bottombar1_linearlayout); LinearLayout bottombar2 = (LinearLayout) findViewById(R.id.task_bottombar2_linearlayout); if(flag){ isEditmodeOn = true; // make topbar and bottombar2 visilble, but bottombar gone topbar.setVisibility(View.VISIBLE); bottombar.setVisibility(View.GONE); bottombar2.setVisibility(View.VISIBLE); // make checkboxes visible in listview visible as well for(int i=0; i&lt;listView.getChildCount(); i++){ LinearLayout ll = (LinearLayout) listView.getChildAt(i); CheckBox cb = (CheckBox) ll.findViewById(R.id.task_row_checkBox1); cb.setVisibility(View.VISIBLE); } } else{ isEditmodeOn = false; topbar.setVisibility(View.GONE); bottombar.setVisibility(View.VISIBLE); bottombar2.setVisibility(View.GONE); // set each checkbox false and its visibility gone for(int i=0; i&lt;listView.getChildCount(); i++){ LinearLayout ll = (LinearLayout) listView.getChildAt(i); CheckBox cb = (CheckBox) ll.findViewById(R.id.task_row_checkBox1); cb.setVisibility(View.GONE); cb.setChecked(false); } } } </code></pre> <p>It works fine but the problem is the application doesn't work when the screen rotates(changes the screen orientation). Everything worked fine as it displayed some layouts but only <code>CheckBoxes in list items. Below is the code in</code>onCreate()`:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.task_layout); initialize(); loadDB(); updateListAdapter(list_title, list_date); // in case of screen rotation if(savedInstanceState != null){ isEditmodeOn = savedInstanceState.getBoolean(EDITMODE_CHECK); isItemChecked = savedInstanceState.getBoolean(ITEM_CHECK); if(isEditmodeOn){ if(!isItemChecked){ Log.i(tag, "item NOT checked"); editmodeSwitch(true); } else{ //this is something different so please don't mind deditmodeSwitch(savedInstanceState.getBooleanArray(LIST_CB_CHECK)); } } } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // save values for rotation outState.putBoolean(EDITMODE_CHECK, isEditmodeOn); outState.putBoolean(ITEM_CHECK, isItemChecked); outState.putBooleanArray(LIST_CB_CHECK, list_cb_check); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i(tag, "you're in onRestoreInstanceState()"); // in case of screen rotation if(savedInstanceState != null){ isEditmodeOn = savedInstanceState.getBoolean(EDITMODE_CHECK); isItemChecked = savedInstanceState.getBoolean(ITEM_CHECK); if(isEditmodeOn){ if(!isItemChecked){ Log.i(tag, "item NOT checked"); editmodeSwitch(true); } else{ // this is for something else so please ignore this part editmodeSwitch(savedInstanceState.getBooleanArray(LIST_CB_CHECK)); } } } </code></pre> <p>What I guessed is <strong>the <code>ListView</code> is being loaded at the end</strong>. Therefore, even if the code in <code>onCreate()</code> makes <code>CheckBoxes</code> visible, the <code>CheckBoxes</code> will become invisible again as its initialization in xml will do so. However, I'm stuck here and need your advice to solve this problem. Can anyone help me?</p> <p>Just in case, below is the checkbox code of layout xml file for getview.</p> <pre><code>&lt;CheckBox android:id="@+id/task_row_checkBox1" android:gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /&gt; </code></pre>
    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.
 

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