开发者

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

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开发者_JAVA技巧 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!

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

http://androidsamples.blogspot.com/2009/06/displaying-list-of-video-files-stored.html


When you want to use a custom layout to represent each item, in your adapter you need to inflate your custom layout in your getView method, using a LayoutInflater.

So i would change the following line:

View cellLayout = findViewById(R.id.albums_list_cell);

to:

View cellLayout = LayoutInflater.from(mContext).inflate(R.layout.albums_list_cell,null);

You need to move your albums_list_cell.xml into the layout folder aswell.

You would then also edit how you retrieve the relevant widgets defined in your layout as follows:

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

Hope this helps!


Solved the above problem...note the (TextView) cast on the following lines. This should be a relativelayout cast of course

} else{
      cellLayout = (TextView) convertView;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜