Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does ListView in Android works ? I'm confused
    text
    copied!<p>I was trying a simple list view in android. I got an example from some website. It worked. Code below:</p> <p>Main Layout File: (res/layout/main.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:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/mainListView"&gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>Row File: (res/layout/simplerow.xml)</p> <pre><code>&lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rowTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:textSize="16sp" &gt; &lt;/TextView&gt; </code></pre> <p>Main Activity:</p> <pre><code>package com.windrealm.android; import java.util.ArrayList; import java.util.Arrays; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class SimpleListViewActivity extends Activity { private ListView mainListView ; private ArrayAdapter&lt;String&gt; listAdapter ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Find the ListView resource. mainListView = (ListView) findViewById( R.id.mainListView ); // Create and populate a List of planet names. String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"}; ArrayList&lt;String&gt; planetList = new ArrayList&lt;String&gt;(); planetList.addAll( Arrays.asList(planets) ); // Create ArrayAdapter using the planet list. listAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.simplerow, planetList); // Add more planets. If you passed a String[] instead of a List&lt;String&gt; // into the ArrayAdapter constructor, you must not add more items. // Otherwise an exception will occur. listAdapter.add( "Ceres" ); listAdapter.add( "Pluto" ); listAdapter.add( "Haumea" ); listAdapter.add( "Makemake" ); listAdapter.add( "Eris" ); // Set the ArrayAdapter as the ListView's adapter. mainListView.setAdapter( listAdapter ); } } </code></pre> <p>I was trying to add an image, not even dynamic. I just thought of adding a layout into the <code>simplerow.xml</code> file. But its not working.</p> <p>Code below:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:contentDescription="@string/songs" android:src="@drawable/song_bg" /&gt; &lt;ImageView android:id="@+id/albumArt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:contentDescription="@string/album" android:src="@drawable/albumart_shanghai" /&gt; &lt;TextView android:id="@+id/rowTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/albumArt" android:layout_toRightOf="@+id/albumArt" android:layout_marginLeft="10dp" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@android:color/black" /&gt; &lt;ImageButton android:id="@+id/imageView3" android:layout_width="48dp" android:layout_height="48dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@null" android:contentDescription="@string/arrow" android:src="@drawable/arrow_icon" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>We can't add the Relative layout inside the ListView ? Pls help me I'm struck with ListView from one month. I couldn't even move a single step :( I'm newbie to java and XML. Pls someone show me the right path.</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