Note that there are some explanatory texts on larger screens.

plurals
  1. POCheckboxes all showing the same text after switching orientations
    primarykey
    data
    text
    <p><strong>Edit: See the accepted answer. Lesson: Sometimes views will save and restore their state automatically. This happens AFTER onCreate. This can cause the overwriting of stuff you did in onCreate. If you don't have unique ids, all views of a certain kind (in my case textboxes) can be overwritten with the same saved state.</strong> (ps: thanks for your help everyone!)</p> <p>So, I have a simple linear layout and I want to add some views that have checkboxes with images. Everything works fine until I switch the orientation of my android phone. When I do it goes back through the onCreate but this time the checkboxes all end up with the same text. Weirdly, the images appear fine. </p> <p>My question is: why is it doing this and how can I make it appear like the first time everytime?</p> <p>In case that makes no sense here's an example: (Edit: It turns out it always shows the last element's text)</p> <p>What I see at first</p> <pre><code>[] a *a's image* [] b *b's image* [] c *c's image* [] d *d's image* </code></pre> <p>Then, after rotating my phone, it redraws</p> <pre><code>[] d *a's image* [] d *b's image* [] d *c's image* [] d *d's image* </code></pre> <p>My original code is pretty complex, but i constructed the following that demonstrates the problem.</p> <p>Main.java:</p> <pre><code>public class Main extends Activity { ArrayList&lt;AnswerView&gt; answers = new ArrayList&lt;AnswerView&gt;(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView title = (TextView)findViewById(R.id.questionText); title.setText("This is a test"); HashMap&lt;String, Drawable&gt; answerInfo = new HashMap&lt;String, Drawable&gt;(); Resources res = getResources(); answerInfo.put("a", res.getDrawable(R.drawable.flower_orange)); answerInfo.put("b", res.getDrawable(R.drawable.flower_white)); answerInfo.put("c", res.getDrawable(R.drawable.leaf)); answerInfo.put("d", res.getDrawable(R.drawable.flower_yellow)); setBoxes(answerInfo); } private void setBoxes(HashMap&lt;String, Drawable&gt; answerInfo) { LinearLayout answerList = (LinearLayout)findViewById(R.id.answerlist); AnswerView cb = null; //Remove all existing answer views answerList.removeAllViews(); answers.clear(); //For each possible answer create a answer views for (String s : answerInfo.keySet()) { cb = new AnswerView(this, s, answerInfo.get(s)); answers.add(cb); String text = cb.getText(); answerList.addView(cb); } } } </code></pre> <p>AnswerView.java</p> <pre><code> public class AnswerView extends RelativeLayout { private CheckBox m_checkbox; private ImageView m_image; //private Context m_context; public AnswerView(Context context, String answer, Drawable d) { super(context); LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.image_checkbox, this, true); m_checkbox = (CheckBox) view.findViewById(R.id.image_checkbox_cb); m_image = (ImageView) view.findViewById(R.id.image_checkbox_img); //m_context = context; m_checkbox.setText(answer); m_image.setImageDrawable(d); m_image.setVisibility(VISIBLE); } public void setChecked(boolean checked) { m_checkbox.setChecked(checked); } public boolean isChecked() { return m_checkbox.isChecked(); } public String getText() { return m_checkbox.getText().toString(); } } </code></pre> <p>Main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dip"&gt; &lt;TextView android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/questionText" android:textSize="18sp"/&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/answerlist"/&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"&gt; &lt;Button android:layout_width="200dip" android:layout_height="wrap_content" android:text="Enter" android:id="@+id/buttonAnswerEnter"/&gt; /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; &lt;/ScrollView&gt; </code></pre> <p>image_checkbox.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"&gt; &lt;CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image_checkbox_cb"&gt;&lt;/CheckBox&gt; &lt;ImageView android:id="@+id/image_checkbox_img" android:layout_width="100dip" android:layout_height="100dip" android:visibility="gone"&gt;&lt;/ImageView&gt; &lt;/LinearLayout&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.
    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