Question

I have A code like this:

public List<Track> getAllSdCardTracks(Context context) {
        List<Track> tracks = new List<Track>();

        Cursor c = context //this is the problem
            .getContentResolver()
            .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.ALBUM_ID }, "1=1",
                null, null);
        if (c.moveToFirst()) {
            do {
                String mArtist = c
                    .getString(c
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                String mTitle = c
                    .getString(c
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                String mId = c.getString(c
                    .getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                String mAlbumName = c
                    .getString(c
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                String mAlbumID = c
                    .getString(c
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

                String mPath = c
                    .getString(c
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                long mDuration = c
                    .getLong(c
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                String mFormattedDuration = DateUtils
                    .formatElapsedTime(mDuration / 1000);
                Track track = new SDTrack(mAlbumID, mAlbumName, mArtist, mId, mTitle, mPath , mFormattedDuration);
                tracks.add(track);
            } while (c.moveToNext());
            if (c != null)
                c.close();
        }

        return tracks;
    }

when I want to make this prj, xamarin says "The type or namespace 'Cursor' Could not be found" and etc. I want to declare Cursor. some one says me: Try reading this docs.xamarin.com/guides/android/platform_features/… "Cursor" is an Android class.. Sounds like you should declare it with the interface ICursor and then get it:

var loader = new CursorLoader(activity, uri, projection, null, null, null);    
var cursor = (ICursor)loader.LoadInBackground();

But I don't undrestand what should I do!! :/ can anybody explain it for me?! (please type codes in c# I can't work with java)

Was it helpful?

Solution

It should be fairly simple:

        var cursor = context.ContentResolver.Query(
            MediaStore.Audio.Media.ExternalContentUri,
            new string[] 
            { 
                /* insert fields here */ 
            }, 
            "1=1",
            null, 
            null);

Returned 'cursor' will be ICursor.

OTHER TIPS

seems like you are missing a using-directive?

Edit: as per Xamarin-Example here

using Android.App;
using Android.Database;
using Android.OS;
using Android.Widget;
using Android.Content;
using Android.Net;

And you need to declare it ICursor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top