Query the android Mediastore for Artist names of Music files - android

I'm torn about how to implement this because Content Provider URI querys do not support the simple SQL "DISTINCT" query method to return a cursor to the Artists of the songs in the mediastore, removing any duplicate entries.
I can query and get a cursor to all the artists, I'm just torn as to how to remove the dupes, or simply not show them.
I've tried using matrixcursor to construct a new cursor with the dupe entries removed, but it's slow (build an array of artists, if in that array don't copy to the new matrixcursor, etc.)
Can anyone recommend a better solution, or point me in the proper direction??
I've also thought about pre-loading the information i need into an array of objects - I'm just concerned with memory overhead in my application.
Thank You for any help you may provide.

The easiest way to get a list of all the artist (and albums is the same method) is to use the MediaStore.Audio.Artist for the quest. For Example something like this would get and show all the artist:
String[] proj = {MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };
musiccursor = managedQuery(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj, null, null, MediaStore.Audio.Artists.ARTIST + " ASC");
String[] from= new String[]{ MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };
int[] to = new int[] { R.id.songname, R.id.rowlength, R.id.rowartist };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.musicrow, musiccursor, from, to);
SongsView.setAdapter(adapter);
Where SongsView would be your list to display them in. Everything else is using a simple Cursor adapter
Hope this helps

You might want to try creating stateful CursorWrapper, overriding the appropriate methods. Simply ensure every call to next() iterates through the cursor until it finds an artist name you consider appropriately unique, optionally storing seen artists in an ArrayList instance variable.

ArrayList<SongBeen> genresList=new ArrayList<SongBeen>();
String value=search+ "%";
String[] inValue=new String[] {value};
ContentResolver musicResolver = activity.getContentResolver();
/*Uri musicInUri = android.provider.MediaStore.Audio.Genres.INTERNAL_CONTENT_URI;
Cursor mInternalCursor = musicResolver.query(musicInUri, null, null, null, null);
*/
Uri musicExUri = android.provider.MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
Cursor mExternalCursor = musicResolver.query(musicExUri, null, MediaStore.Audio.Genres.NAME+ " like ?",
inValue, "LOWER(" + MediaStore.Audio.Genres.NAME + ") ASC");
Cursor[] cursors = {mExternalCursor};
final MergeCursor mMergeCursor = new MergeCursor(cursors);
if (mMergeCursor.moveToFirst()) {
do {
SongBeen been=new SongBeen();
long thisId= mMergeCursor.getLong(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID));
String thisTrack = mMergeCursor.getString(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME));
been.setId(thisId);
been.setTrack(thisTrack);
genresList.add(been);
} while (mMergeCursor.moveToNext());
}
mMergeCursor.close();
return genresList;

Related

Content Resolver - How to sum up a column value for all rows using query

I am working on a project using Content Provider for DB.
I am able to fetch all rows using the query mentioned below.
My problem is I want to sum up a column for all rows fetched.
I am querying as :
String query = WorkTable.ENTRY_TIME + " = ?"
String projection = "new String[]{WorkoutLogTable.STEPS}";
Cursor cursor = getContentResolver()
.query(
LogProvider.WORK_LOG,
projection,
query,
new String[]{dateString},
null
);
I want the sum of WorkoutLogTable.STEPS. Projection needs string[] as parameter, so how can I sum up the STEPS value?
Edit
I used a projection:
String projection = new String[]{"sum(WorkoutLogTable.STEPS}) as total"};
But it's also not working.
Solution:
I was doing a mistake by making the whole part as string.
So I have resolved so:
String projection = new String[]{"sum(steps) as total"}; // steps is my column name and I was fetching it by WorkoutLogTable.STEPS which was wrong
Or another solution can be using Dynamic string (from cricket_007 answer)
Projection needs string[] as parameter,
Right, so why is your projection variable a String? This statement won't even compile.
String projection = "new String[]{"sum(WorkoutLogTable.STEPS}) as total"};
Maybe you meant this?
String[] projection = new String[] { "sum(" + WorkoutLogTable.STEPS + ") as total" };
You need to use an actual String[] object, not a String that has the content of "String[] { ... }"
String selection = WorkTable.ENTRY_TIME + " = ?"
String[] projection = new String[] { "sum(" + WorkoutLogTable.STEPS + ")" };
String[] selectionArgs = new String[] { dateString };
Cursor cur = getContentResolver().query(
LogProvider.WORK_LOG,
projection,
selection,
null, null);
Is this a content provider from another app? If you are writing the content provider, I would recommend that you add another URL specifically for the summary query and do your sum function in the query inside the content provider. Then just use the alternate URL when you go through the content resolver.

How to use limit and offset to page through the data

I am new to SQLite and I don't know how to use limit and offset to select a limit number of data from the database, I mean I know the query phrase, but how to use it in a cursor so I can get those data into a listview?
Currently I am using the code below to query data from the database and show them in a listview but it seems I query too much data for one query and the SQLite fail to grow, so I want to split the query into some smaller ones and do it in one time,someone suggested me to try limit and offset,but I googled it there are really not much about it on the Internet.
Would somebody kindly provide me the guide to this? an example or a tutoral, anything will do,thx
channellist = (ListView) findViewById(R.id.Channel);
mDB = new ChannelDB(this);
String[] columns = {mDB.KEY_ID, mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_PATH, mDB.KEY_DBLINK};
String table = mDB.channelS_TABLE;
c = mDB.getHandle().query(table, columns, null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.channelview,
c,
new String[] {mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_DBLINK},
new int[] {R.id.poster, R.id.channel, R.id.douban});
adapter.setViewBinder(new ChannelViewBinder(this));
channellist.setAdapter(adapter);
pass the last argument with number as string like you need fetch 10 then you can do like this way
c = mDB.getHandle().query(table, columns, null, null, null, null, null,"10");
for more reference see How to use the LIMIT argument in an SQLite Query with Android

What is the efficient way to get all contacts names and phone numbers in the contact API

I currently use the old contacts API (that was deprecated on Android 2.0), and I am wondering if the is a way to get all contacts along with their's phone number, without making a separate query for each contact as was advised in a few sites I found.
for instance in the old API, I could do something like this:
String[] projection = new String[] { Phones._ID, Phones.NAME,
Phones.NUMBER };
Uri contacts = Phones.CONTENT_URI;
Cursor managedCursor = managedQuery(contacts,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
// Put the results in ascending order by name
Phones.NAME + " ASC");
thanks.
I found what I was looking for, and using the new API is even simpler, here is the new way of querying all the contact along with their names and phone number:
Cursor managedCursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, Phone.DISPLAY_NAME + " ASC");

Android: Displaying contact names in a Spinner

I'm trying to simply get contacts from the Device phone book and display them on a Spinner, the code:
// Form an array specifying which columns to return.
String[] PROJECTION = new String[] {
People._ID, People.NAME
};
// Get the base URI for the People table in the Contacts content provider.
Uri contacts = People.CONTENT_URI;
Spinner contactsSpinner = (Spinner) findViewById(R.id.recipient_names);
// Make the query.
Cursor contactsCursor = managedQuery(contacts,
PROJECTION, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
// Put the results in ascending order by name
People.NAME + " ASC");
Log.e("EE", String.valueOf(contactsCursor.getCount()));
SimpleCursorAdapter sca = new SimpleCursorAdapter(
this, android.R.layout.simple_spinner_item,
contactsCursor, new String[] {People.NAME}, new int[] {android.R.id.text1}
);
Log.e("EE", String.valueOf(sca.getCount()));
sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
contactsSpinner.setAdapter(sca);
Compiles and runs fine, just that... The logs is showing 1 for both Log.e() call, which I think imply that contacts were actually retrieved successfully, however the Spinner is empty... Could anyone tell me I am doing wrong?
Turns out that it's not supported on sdk > 2.0

Android SDK - List All Users

I have just begun working with the Android SDK and I am having problems with my first app. Currently, I am trying to list all of the users in a big list. However, no matter what I try, the app continues to force close. I found the code in the sample files, but this is still giving me issues. Below is the code I am using.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] {
People._ID,
People._COUNT,
People.NAME,
People.NUMBER
};
//Get the base URI for the People table in the Contacts content provider.
Uri contacts = People.CONTENT_URI;
//Make the query.
Cursor managedCursor = managedQuery(contacts,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
// Put the results in ascending order by name
People.NAME + " ASC");
Cursor c = getContentResolver().query(Contacts.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
String[] columns = new String[] {People.NAME};
int[] names = new int[] {R.id.text1};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.main, c, columns, names);
setListAdapter(mAdapter);
}
This is directly from the sample file, yet it still errors out. I have found that the line that causes the problem is the "Cursor managedCursor = managedQuery(contacts," line. Has anyone else out there seen this? I am at a loss and have not found any solutions after 2 hours or research.
Also, I have added the following line to my app's manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
Thanks, and if you need more information please let me know.
I believe the example from the SDK docs is out of date. Try getting rid of the People._COUNT column from the cursor projection.
It's probably causing an IllegalArgumentException (see the output from adb logcat)

Categories

Resources