Android: Use XML Layout for List Cell rather than Java Code Layout (Widgets)
<p><br>
I'm in the process of making a music app and I'm currently working on the library functionality. I'm having some problems, however, in working with a list view (In particular, the cells). I'm trying to move from a simple textview layout in each cell that's created within java to one that uses an XML file for layout (Hence keeping the Java file mostly semantic)<br>
This is my original code for the cell layout:</p>

<pre><code>public View getView(int position, View convertView, ViewGroup parent) {

        String id = null;
        TextView tv = new TextView(mContext.getApplicationContext());
        if (convertView == null) {
            music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
            musiccursor.moveToPosition(position);
            id = musiccursor.getString(music_column_index);

            music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            musiccursor.moveToPosition(position);
            id += "\n" + musiccursor.getString(music_column_index);

            music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
            musiccursor.moveToPosition(position);
            id += "\n" + musiccursor.getString(music_column_index);

                tv.setText(id);
          } else
                tv = (TextView) convertView;

    return tv;
    }
</code></pre>

<p>And my new version:</p>

<pre><code>public View getView(int position, View convertView, ViewGroup parent) {

        View cellLayout = findViewById(R.id.albums_list_cell);
        ImageView album_art = (ImageView) findViewById(R.id.album_cover);
        TextView album_title = (TextView) findViewById(R.id.album_title);
        TextView artist_title = (TextView) findViewById(R.id.artist_title);

        if (convertView == null) {
            music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
            musiccursor.moveToPosition(position);
            album_title.setText(musiccursor.getString(music_column_index));

            //music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            //musiccursor.moveToPosition(position);

            music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
            musiccursor.moveToPosition(position);
            artist_title.setText(musiccursor.getString(music_column_index));
        } else{
            cellLayout = (TextView) convertView;
        }

    return cellLayout;
    }
</code></pre>

<p>The initialisation (done in the on create file):</p>

<pre><code>musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(this));
musiclist.setOnItemClickListener(musicgridlistener);
</code></pre>

<p>And the respective XML files:</p>

<p>(main)</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:id="@+id/PhoneMusicList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    /&gt;
    &lt;TextView 
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        android:text="@string/no_list_data"
        /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>(albums_list_cell)</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/albums_list_cell"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"&gt;
    &lt;ImageView
    android:id="@+id/album_cover"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_width="50dip"
    android:layout_height="50dip"
    /&gt;
    &lt;TextView
        android:id="@+id/album_title"
        android:layout_toRightOf="@+id/album_cover"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    /&gt;
    &lt;TextView
        android:id="@+id/artist_title"
        android:layout_toRightOf="@+id/album_cover"
        android:layout_below="@+id/album_title"
        android:layout_width="wrap_content"
        android:layout_height="15dip"
    /&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>In theory (based on the tiny bit of Android I've done so far) this should work..it doesn't though. Logcat gives me a null pointer exception at line 96 of the faulty code, which is the album_title.setText line. It could be a problem with my casting but Google tells me this is ok :D</p>

<p>Thanks for any help and let me know if you need more info!</p>

<p>EDIT: Just to point out, the code this section is based on is from, most of is the same as mine for now</p>

<p><a href="http://androidsamples.blogspot.com/2009/06/displaying-list-of-video-files-stored.html" rel="nofollow">http://androidsamples.blogspot.com/2009/06/displaying-list-of-video-files-stored.html</a></p>
