Android: Use XML Layout for List Cell rather than Java Code Layout (Widgets)

Posted by Stephen Finucane on Stack Overflow See other posts from Stack Overflow or by Stephen Finucane
Published on 2011-02-19T23:23:10Z Indexed on 2011/02/19 23:25 UTC
Read the original article Hit count: 389

Filed under:
|

Hi,
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)
This is my original code for the cell layout:

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;
    }

And my new version:

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;
    }

The initialisation (done in the on create file):

musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(this));
musiclist.setOnItemClickListener(musicgridlistener);

And the respective XML files:

(main)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/PhoneMusicList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
    <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"
        />
</LinearLayout>

(albums_list_cell)

<?xml version="1.0" encoding="utf-8"?>
<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">
    <ImageView
    android:id="@+id/album_cover"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_width="50dip"
    android:layout_height="50dip"
    />
    <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"
    />
    <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"
    />
</RelativeLayout>

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

Thanks for any help and let me know if you need more info!

© Stack Overflow or respective owner

Related posts about java

Related posts about android