Android: Error retrieving songs from a playlist - android

I'm having trouble retrieving songs from playlists in Android. I am currently attempting to achieve this using the following code (based on an answer to this question given here):
String where = MediaStore.Audio.Playlists._ID
+ "=?";
String whereVal[] = {id};
String[] proj =
{
MediaStore.Audio.Playlists.Members.AUDIO_ID,
MediaStore.Audio.Playlists.Members.ARTIST,
MediaStore.Audio.Playlists.Members.TITLE,
MediaStore.Audio.Playlists.Members._ID
};
Cursor mCursor = getActivity().getContentResolver().query(
MediaStore.Audio.Playlists.Members.getContentUri("external", Long.parseLong(id)),
proj, where, whereVal, null);
where id is the playlist ID that I retrieve using cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists._ID)); (and I know that this is correct)
However this is causing the following error:
07-06 19:35:02.496: E/AndroidRuntime(8818): android.database.sqlite.SQLiteException:
ambiguous column name: _id (code 1): , while compiling: SELECT audio_id, artist, title,
audio_playlists_map._id AS _id FROM audio_playlists_map, audio WHERE (audio._id =
audio_id AND playlist_id=?) AND (_id=?)
I have a funny feeling that the problem lies in the following:
String where = MediaStore.Audio.Playlists._ID + "=?"; but I have no clue how to rectify it.

You don't need the where clause since you already use the content URI of the playlist. I would try this:
Cursor mCursor = getActivity().getContentResolver().query(
MediaStore.Audio.Playlists.Members.getContentUri("external", Long.parseLong(id)),
proj, null, null, null);

Related

CalendarContract.Events._ID is missing in Android 5.0

My app queries a particular event in CalendarContract.Events using Events._ID. This worked well until attempting to run it on a 5.0 device, now I get an exception
01-12 17:28:50.525: E/Unknown Source(18499): android.database.sqlite.SQLiteException:
no such column: CalendarContract.Events._ID (code 1): ,
while compiling:
SELECT _id, account_type, title, organizer, description, eventLocation,
hasAlarm, calendar_id
FROM view_events
WHERE (lastSynced = 0 AND (CalendarContract.Events._ID=1))
Querying all columns in Events indeed does not return _ID. Any idea why this has been removed or if it's a bug? I can't seem to find away to uniquely identify events any more.
Here is my query:
String[] projection = new String[]{Events._ID, Events.ACCOUNT_TYPE, Events.TITLE,
Events.ORGANIZER, Events.DESCRIPTION, Events.EVENT_LOCATION, Events.HAS_ALARM,
Events.CALENDAR_ID};
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
"CalendarContract.Events._ID=" + eventId, null, null);
Thanks for any information!
The answer of #Andrew isn't right.
You are wrongly using the selectionClause and the selectionArgs parameters.
Here is what you are doing:
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
CalendarContract.Events._ID + "=" + eventId, null, null);
And here is what you should do:
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
CalendarContract.Events._ID + " = ?", new String[]{eventId}, null);
The eventId needs to be passed within the selectionArgs so that the contentResolver can build the query statement for you, chaining the selectionArgs and substituting them to the ? char in the selectionClause parameter.
The question is three years old, but I hope my answer can help someone else.

Contacts are not sorting in Android

I have inserted some raw contacts (given account type and name null). the native contact app of android shows all contacts are sorted ( merged previews and newly given). But in my app (a listview for displaying contacts), it shows first the previous contatcs (sorted by display name) and then newly inserted contacs (also sorted). I have tried whatever combination possible , but no luck. please help any one.
Query Code
String PROJECTION[] = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
private final String SORT_ORDER = ContactsContract.Contacts.DISPLAY_NAME + " ASC";
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor contacts = cr.query(uri, PROJECTION, null ,null, SORT_ORDER);
Update*strong text*
however , i was using handler , and now after converting to cusorloadr with loader manager. problem solved
Use getShort() method of Cursor to sort on the bases of a particular column.
try this as query :
Cursor cursor = getContentResolver.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, null ,null, Phone.DISPLAY_NAME + " ASC");

Android: query for albums gives duplicates

I'm querying the contentresolver in order to list all the albums on the device. However I have losts of duplicate. Is it a problem of my query or of the android database?
Maybe is it because I'm getting albums from a songs' table?
String[] projection = {
MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ARTIST
};
String selection = null;
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
Thank you

sql where clause for managedQuery android method

I am trying to write the parameters to the managedQuery method in android and having trouble with the WHERE clause, here is what i wrote;
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DATA = filename, proj, null );
"filename" is a string variable holding the path of an image, example /mnt/sdcard/pic05.png
I want it to return a cursor holding a record that is the same record for the pic05.png image and the cursor returned would hold the TITLE column information for this specific picture. How did i mess up the sql WHERE CLAUSE? I thought that my syntax must be wroing on the where clause
Add ' ' around filename. Try this
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, selection, null, null );

Invalid column in contacts

I am planning to create cursorjoiner for my app. I would like to join the Display name and Email id using the cursorjoiner. I am trying on Android 1.6.
When I query for the Email Id list, I get an exception as :
03-30 13:08:15.609: ERROR/AndroidRuntime(302): Caused by: java.lang.IllegalArgumentException: Invalid column person
My code is as below
String[] projection1 = new String[] {
People._ID,
People.DISPLAY_NAME
} ;
String[] projection2 = new String[] {
Contacts.ContactMethods.PERSON_ID,
Contacts.ContactMethods.DATA
} ;
Cursor cur = cr.query(People.CONTENT_URI, projection1, null, null, null);
Cursor emailCur = cr.query( Contacts.ContactMethods.CONTENT_EMAIL_URI,
projection2,
null, null, null);
I checked the docs it says PERSON_ID is a valid entry.
Also, if I use the same column as a parameter inside a query it works.
emailCur = cr.query( Contacts.ContactMethods.CONTENT_EMAIL_URI,
projection2,
Contacts.ContactMethods.PERSON_ID + " = ?",
new String[]{id}, null);
Can any one tell what parameter should I use in projection to achieve this.
Contacts is depreciated, use ContactsContract instead.

Categories

Resources