使用LoaderManager和CursorLoader在列表视图中显示专辑和艺术家

时间:2012-06-14 00:46:12

标签: android android-listfragment android-music-player android-cursorloader

我有一个ListFragment,其列表应使用MediaStore显示设备的相册和关联的艺术家。因此每行有两个TextViews。 我正在使用LoaderManagerCursorLoader填充列表,使用自定义CursorAdapter将行的TextViews绑定到数据。

ListFragment代码:

public class AlbumsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

AlbumsAdapter mAdapter;

        @Override
           public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           View myFragmentView = inflater.inflate(R.layout.albums_fragment_layout, container, false);
           return myFragmentView;
         }


        @Override
          public void onActivityCreated(Bundle savedInstanceState) {
           super.onActivityCreated(savedInstanceState);

                    setListAdapter(mAdapter);
                    getLoaderManager().initLoader(0, null, this);                               
        }


static final String[] ALBUM_SUMMARY_PROJECTION = { MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ARTIST}; 



        public Loader<Cursor> onCreateLoader(int id, Bundle args) {      
            String select = null;  
            return new CursorLoader(getActivity(), MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,ALBUM_SUMMARY_PROJECTION, select, null, null);  
        }  



        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {  
            mAdapter.swapCursor(data);  
        }  



        public void onLoaderReset(Loader<Cursor> loader) {  
            mAdapter.swapCursor(null);  
        }  
    }

AlbumsAdapter(Custom CursorAdapter)代码:

public class AlbumsAdapter extends CursorAdapter {

    private final LayoutInflater mInflater;

     public AlbumsAdapter(Context context, Cursor c) {
        super(context, c);
        mInflater=LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView albumTitle =(TextView)view.findViewById(R.id.albumTextView);
        albumTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));

        TextView artistName=(TextView)view.findViewById(R.id.artistTextView);
        artistName.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)));

    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final View view=mInflater.inflate(R.layout.albums_row,parent,false); 
        return view;
    }

目前,当我显示这个ListFragment时,我有一个java.lang.NullPointerException,这里是logCat:

 06-14 09:39:23.109: W/dalvikvm(8632): threadid=1: thread exiting with uncaught exception (group=0x40c2d1f8)
06-14 09:39:23.109: E/AndroidRuntime(8632): FATAL EXCEPTION: main
06-14 09:39:23.109: E/AndroidRuntime(8632): java.lang.NullPointerException
06-14 09:39:23.109: E/AndroidRuntime(8632):     at com.music.pod.AlbumsFragment.onLoadFinished(AlbumsFragment.java:61)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at com.music.pod.AlbumsFragment.onLoadFinished(AlbumsFragment.java:1)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:438)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:406)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.content.Loader.deliverResult(Loader.java:125)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.content.CursorLoader.deliverResult(CursorLoader.java:88)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.content.CursorLoader.deliverResult(CursorLoader.java:42)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:236)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:76)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.os.AsyncTask.finish(AsyncTask.java:602)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.os.Looper.loop(Looper.java:137)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at android.app.ActivityThread.main(ActivityThread.java:4507)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at java.lang.reflect.Method.invoke(Method.java:511)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-14 09:39:23.109: E/AndroidRuntime(8632):     at dalvik.system.NativeStart.main(Native Method)

是否可以帮我修正我的代码,以便将相册和相关艺术家显示在列表视图中?

解决问题:

对于任何试图用Album,Artist等填充自定义列表视图的人......使用LoaderManager / CursorLoader技术你应该:

1)创建自己的Custom CursorAdapter,它将行的TextViews绑定到Cursor的数据(参见上面的Custom CursorAdapter)

2)然后,在你的片段中,这是做什么的:

public class AlbumsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

AlbumsAdapter mAdapter;

        @Override
           public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           View myFragmentView = inflater.inflate(R.layout.albums_fragment_layout, container, false);
           return myFragmentView;
         }


        @Override
          public void onActivityCreated(Bundle savedInstanceState) {
           super.onActivityCreated(savedInstanceState);

                    mAdapter = new AlbumsAdapter(getActivity(), null);
                    setListAdapter(mAdapter);
                    getLoaderManager().initLoader(0, null, this);                               
        }


static final String[] ALBUM_SUMMARY_PROJECTION = { MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ARTIST};  



        public Loader<Cursor> onCreateLoader(int id, Bundle args) {      
            String select = null;  
            return new CursorLoader(getActivity(), MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,ALBUM_SUMMARY_PROJECTION, select, null, null);  
        }  



        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {  
            mAdapter.swapCursor(data);  
        }  



        public void onLoaderReset(Loader<Cursor> loader) {  
            mAdapter.swapCursor(null);  
        }  
    }

就是这样!我不知道为什么没有人为了填充自定义列表视图而采购了一个非常简单的例子,关于如何使用LoaderManager,CursorLoader和Custom适配器。无论如何,我希望这将有助于像我这样的初学者。

1 个答案:

答案 0 :(得分:2)

我认为您的mAdapternull(我不知道您初始化它的位置,如果您初始化它),当您尝试交换光标时,它会抛出该异常。

如果您没有初始化AlbumAdapter引用,那么您应该在设置适配器之前执行此操作:

mAdapter = new AlbumsAdapter(getActivity(), null);
setListAdapter(mAdapter);