GetcontentResolver (); equivalent

I'm trying to use a java code in xamarin with C #, I was doing well until I got to that part:

Inside this method, create a ContentResolver instance, retrieve the URI for external music files, and create a Cursor instance using the ContentResolver instance to query the music files:

ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

Can anyone help me to adapt this part?

Answers

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I would say this looks like a question better suited for the Xamarin Android forum, as it appears to be about just Android, no Forms involved.

    That said, getter methods are usually properties in Xamarin Android. GetContentResolver is a method on a context it says, so from the Activity or something with a context, try looking for a ContentResolver property. If I am in my MainActivity, typing this.ContentResolver gets me an instance of a ContentResolver.

  • @AdamMeaney said:
    I would say this looks like a question better suited for the Xamarin Android forum, as it appears to be about just Android, no Forms involved.

    That said, getter methods are usually properties in Xamarin Android. GetContentResolver is a method on a context it says, so from the Activity or something with a context, try looking for a ContentResolver property. If I am in my MainActivity, typing this.ContentResolver gets me an instance of a ContentResolver.

    So I understood what you said, so in mainactivity I stated:

    ContentResolver getContentResolver;

    So in the line of code I put it and it was, but the problem is that when I put the (), to return the list it does not accept and it turns red.

    ContentResolver musicResolver = getContentResolver();

    It seems that it is a problem in my version of xamarin, since I have researched a lot and it seems that this problem does not exist in others, there they can get getContentResolver (), without problems, in my nor does this component appear.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I think we had a miscue somewhere.

    In native Android, getContentResolver is a method, part of the Context class.

    In Xamarin Android, it is a property of ContentResolver that can be accessed on any Context instance. Here is an example post on it.

  • I also think because I tried this command, I did not give it a mistake, either.

  • I use all these:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.Provider;
    using Android.App;
    using Android.Database;
    using Android.Net;
    using Android.App;
    using Android.Provider;
    using Android.Util;
    using Android.Views;
    using Java.IO;
    using Java.Util;

  • DavidOrtinauDavidOrtinau USForum Administrator, Xamarin Team, Insider, University Xamurai

    @Jair_xamarin is there a reason you're still not posting in the Android forum where you'll get the best help for this topic (https://forums.xamarin.com/categories/android)?

    The C# equivalent to the Java you posted should look something like this in an Android Activity class.

    var musicResolver = this.ContentResolver; var musicUri = Android.Provider.MediaStore.Audio.Media.ExternalContentUri; var musicCursor = musicResolver.Query(musicUri, null, null, null, null);

    I recommend you read the Stack Overflow link that @AdamMeaney shared.

  • Sorry, where can I move this topic to android category?

    The line you passed worked, the others I had already adapted:
    Android.Net.Uri MusicUri = MediaStore.Audio.Media.ExternalContentUri;
    ICursor musicCursor = musicResolver.Query(MusicUri, null, null, null, null);

    But will the first line work even without the ()?

    Here is the code I am adapting:
    https://code.tutsplus.com/tutorials/create-a-music-player-on-android-project-setup--mobile-22764

  • DavidOrtinauDavidOrtinau USForum Administrator, Xamarin Team, Insider, University Xamurai

    I don't think you can move it, which is why it's best to post in the right place initially. Maybe a moderator can move it, I don't know.

    But will the first line work even without the ()?

    Run it and see. :)

    You do not need the (). This is an idiomatic change made by Xamarin to make it more C#.

  • Jair_xamarinJair_xamarin BRMember
    edited December 2016

    Thank you now everything is without error, I have compiled and gives installation error, I will seek a solution

  • I have two doubts in a snippet of this code:

    Collections.sort(songList, new Comparator<Song>(){ public int compare(Song a, Song b){ return a.getTitle().compareTo(b.getTitle()); } });

    I tried to adapt it this way:Collections.Sort(songlist, new Comparer<Song>(){}

    But he did not accept, I also tried with Icomparator, but due to not having the <> he did not accept.

    My other question is about the syntax of the code as I noted the following:

    () {} 0;

    Is this really right?

Sign In or Register to comment.