CursorAdapter的newView函数不会被触发

时间:2014-11-25 03:19:49

标签: android video cursor android-cursoradapter

使用CursorAdapter时无法触发newView函数

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_local_screen,
            container, false);


    Bundle data = new Bundle();
    data.putStringArray("PROJECTION", new String [] { 
        MediaStore.Video.VideoColumns._ID,
        MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME, 
        MediaStore.Video.VideoColumns.BUCKET_ID,
        MediaStore.Video.VideoColumns.DATE_TAKEN,
        MediaStore.Video.VideoColumns.DATE_ADDED,
        MediaStore.Video.VideoColumns.DESCRIPTION,
        MediaStore.Video.VideoColumns.DURATION});

    data.putString("SELECTION", null);
    data.putStringArray("SELECTIONARRAY", null);
    data.putString("SORTORDER", MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME + " LIMIT " + mLimit);

    VideoCursorAdapter adapter = new VideoCursorAdapter(getActivity(),null,true);
    LocalVideos videos = new LocalVideos(getActivity(), adapter);
    getLoaderManager().initLoader(LocalVideos.LoaderID.THUMBNAIL.getID(), data, videos);

    ListView listView = (ListView)rootView.findViewById(R.id.listVideos);
    listView.setAdapter(adapter);

    videos.setView(listView);

上面的代码我设置了加载mediastore数据的参数。使用videoCursorAdapter设置listView。

在LoadVideos中,我有一个游标加载器来加载游标并将其传递给游标加载器。

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    // TODO Auto-generated method stub
    if (LoaderID.THUMBNAIL.getID() == arg0) 
        return new CursorLoader(mContext, 
                                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
                                (String[])arg1.getStringArray("PROJECTION"), 
                                arg1.getString("SELECTION"), 
                                (String[])arg1.getStringArray("SELECTIONARRAY"), 
                                arg1.getString("SORTORDER"));
    return null;
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    // TODO Auto-generated method stub
    Log.w("ALBUM", "id="+arg0.getId() + " " + LoaderID.THUMBNAIL.getID() + " " + arg1.getCount());
    if (LoaderID.THUMBNAIL.getID() == arg0.getId())
        mAdapter.swapCursor(arg1); 
    mView.invalidate();
}

代码会在光标中打印10个数据。不知怎的,没有错误。但是newView功能没有被击中。

1 个答案:

答案 0 :(得分:0)

newView并不总是被命中,适配器只在必要时调用newView()。 (例如,交换光标后列表中的项目增加)
(如果您有实现BaseAdapter的经验,您可以认为convertView仅在为null时构造) 您可以查看CursorAdapter的the source code进行确认。