无法使用CursorLoader和ContentProvider填充ListView

时间:2015-01-05 16:42:58

标签: android listview

我正在尝试使用 使用存储在SD卡上的歌曲填充我的,但它会在运行时停止我的应用...希望如何实际实现代码有一些帮助....

package com.example.listofsongs;

import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Media;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity implement LoaderManager.LoaderCallbacks<Cursor>{

SimpleCursorAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,null, new String[]{Media.DISPLAY_NAME}, new int[]{android.R.id.text1}, 0);
    setListAdapter(mAdapter);
    getLoaderManager().initLoader(0, null, this);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
}


static final String[] DISPLAY_NAME={Media.DISPLAY_NAME};

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {

    Uri baseUri;
    baseUri = Media.EXTERNAL_CONTENT_URI;
    return new CursorLoader(this, baseUri, DISPLAY_NAME, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    // TODO Auto-generated method stub
    mAdapter.swapCursor(arg1);
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    // TODO Auto-generated method stub
    mAdapter.swapCursor(null);
}


}

0 个答案:

没有答案