Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I define a ListView with contents in the layout XML?
    text
    copied!<p>I'm prototyping an Android application, and am trying to define the UI before writing any Java code. The basic XML layout stuff works fine, but I can't figure out how to add a <code>ListView</code> with contents. Adding the <code>ListView</code> inside a <code>LinearLayout</code> works fine, but I can't add anything (not even another <code>LinearLayout</code>) as the <em>contents</em> of this <code>ListView</code>.</p> <p>I tried the fairly obvious way of doing this:</p> <pre><code>&lt;ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2"&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;TextView android:text="Line One" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:text="Line Two" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:text="Line Three" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; &lt;/ListView&gt; </code></pre> <p>But it failed (at runtime) with an exception when trying to start the activity:</p> <pre><code>java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView. </code></pre> <p>The SDK documentation for the XML layout language is fairly poor, and I can't seem to find any obvious way to make this work. What am I missing?</p> <p>Bonus question: is there a way to detect problems in the XML layout at <em>build-time</em>, instead of waiting for Android to throw an exception at runtime?</p>
 

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