How do I get the row for ID ?
I try so it is impossible .
case R.id.buttonTest: {
String[] projection = {DbTest.NAME};
String selection = "_id = ?";
String[] selectionArgs = { String.valueOf(1) };
Cursor c = sqdb.query(DbTest.TABLE_NAME,projection,selection,selectionArgs ,null,null,null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(DbTest.NAME));
textView1.setText(name);
}
}
break;
Related
For a given number from my address book, I need to look-up if the number has whatsapp enabled.
(The idea is to choose SMS/WhatsApp for initiating a text intent)
Lets say, I have two numbers under a contact, And I need to know which one has whatsapp enabled.
The "People" app on the Nexus 4 shows both contact numbers,
And also a little below has a CONNECTIONS section, which shows only the WhatsApp possible contact.
Is there a way to look up(like how People app does) ?
If you want to know if this contact has WhatsApp:
String[] projection = new String[] { RawContacts._ID };
String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
String[] selectionArgs = new String[] { "THE_CONTACT_DEVICE_ID", "com.whatsapp" };
Cursor cursor = activity.getContentResolver().query(RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
boolean hasWhatsApp = cursor.moveToNext();
if (hasWhatsApp){
String rowContactId = cursor.getString(0);
}
And to find to which number of this contact has WhatsApp
projection = new String[] { ContactsContract.Data.DATA3 };
selection = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? ";
selectionArgs = new String[] { "vnd.android.cursor.item/vnd.com.whatsapp.profile", rawContactId };
cursor = CallAppApplication.get().getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, "1 LIMIT 1");
String phoneNumber = null;
if (cursor.moveToNext()) {
phoneNumber = cursor.getString(0);
}
Using #idog's method, I improved code to work easier. contactID is a string variable to be passed. If contact hasn't WhatsApp returns null, otherwise returns with contactID which has been passed as variable.
public String hasWhatsapp(String contactID) {
String rowContactId = null;
boolean hasWhatsApp;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
String[] selectionArgs = new String[]{contactID, "com.whatsapp"};
Cursor cursor = getActivity().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
if (cursor != null) {
hasWhatsApp = cursor.moveToNext();
if (hasWhatsApp) {
rowContactId = cursor.getString(0);
}
cursor.close();
}
return rowContactId;
}
public int hasWhatsApp(String contactID) {
int whatsAppExists = 0;
boolean hasWhatsApp;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
String[] selectionArgs = new String[]{contactID, "com.whatsapp"};
Cursor cursor = getActivity().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
if (cursor != null) {
hasWhatsApp = cursor.moveToNext();
if (hasWhatsApp) {
whatsAppExists = 1;
}
cursor.close();
}
return whatsAppExists;
}
I wish to pull out of the second row of the column NAME.
case R.id.buttonTest: {
String[] projection = {DbTest.NAME};
String selection = "_id = ?";
String[] selectionArgs = { String.valueOf(1) };
Cursor c = sqdb.query(DbTest.TABLE_NAME,projection,selection,selectionArgs ,null,null,null);
moveToPosition(2);
String name = c.getString(c.getColumnIndex(DbTest.NAME));
textView1.setText(name);
}
break;
Call moveToPosition on reference to cursor:
c.moveToPosition(int)
If you`ll call
cursor.moveToFirst();
then after
cursor.moveToNext();
, you will move to the second position.
I used this in my code,
while (cursor.moveToNext()){
if(cursor.getPosition() == position){
String sss = cursor.getString(0).toString();
}
}
I am trying to make a query to sqlite android to see for example how many users of a given username exist in a table.
This is my function. I must specify that "getContentResolver() != null" and so is variable name.
private int findSelectedUser(String name) {
int count = 0;
try {
String[] whereArgs = new String[] {name};
String[] PROJECTION = new String[] { MyProvider.SETTINGS_USERNAME };
Cursor c = getContentResolver().query(MyProvider.SETTINGS_URI,
PROJECTION, MyProvider.SETTINGS_USERNAME , whereArgs, null);
if (c != null) {
count = c.getCount();
c.close();
}
} catch (NullPointerException e) {
}
System.out.println("Found something? " + count);
return count;
}
And after running i receive the error from the subject...and don't get it. In my where clause i have one column, in my where arguments one value.
Please help me make some sence of this, Thank you.
I guess that works:
String[] whereArgs = new String[] {name};
String[] PROJECTION = new String[] { MyProvider.SETTINGS_USERNAME };
Cursor c = getContentResolver().query(MyProvider.SETTINGS_URI,
PROJECTION, MyProvider.SETTINGS_USERNAME + "=?" , whereArgs, null);
if (c != null) {
count = c.getCount();
c.close();
}
If you want to use whereArgs you have to have the same amount of ? in the where as you have items whereArgs
whereArgs will replace the ? in the final database query
String where = "name = ? OR name = ?";
String[] whereArgs = new String[] {
"Peter",
"Jim"
};
that results in name = 'Peter' OR name = 'Jim' for the query.
Btw: don't catch(NullPointerException e) - make your code safe so they can't happen
I need to fetch the members for a specific group in android contacts.
I have the contact group names and their ID's
Can anyone provide me how to query the contacts provider for members in a particular group ?
Try this method:
private Cursor getContacts(String groupID) {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
if(groupID != null && !"".equals(groupID)) {
selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ " = ?";
selectionArgs = new String[] { groupID };
}
else
selection = "1) GROUP BY (" + ContactsContract.Data.CONTACT_ID;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC ";
return getContentResolver().query(uri, projection,
selection, selectionArgs, sortOrder);
}
This works on Android 2.3.3 and lower, but doesn't work on Android 4+ and I don't currently know why.
UPD.
Adding custom string parameter "GROUP BY" to SQL query is denied in Android 4+, so I've founded this workaround:
private Cursor getContacts(String groupID) {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
if(groupID != null && !"".equals(groupID)) {
selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ " = ?";
selectionArgs = new String[] { groupID };
}
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC ";
Cursor cursor = getContentResolver().query(uri, projection,
selection, selectionArgs, sortOrder);
MatrixCursor result = new MatrixCursor(projection);
Set<Long> seen = new HashSet<Long>();
while (cursor.moveToNext()) {
long raw = cursor.getLong(1);
if (!seen.contains(raw)) {
seen.add(raw);
result.addRow(new Object[] { cursor.getLong(0),
cursor.getLong(1), cursor.getString(2) });
}
}
return result;
The playlist names can be found by a query on MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI
and then look at the MediaStore.Audio.PlaylistsColumns.NAME column.
There is a data column too, MediaStore.Audio.PlaylistsColumns._DATA, but it is returning null.
The list of songs (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI/id) do not seem to show any playlist affiliation.
Here is a bit from my program:
The gist is that you need the id for the playlist to grab the songs.
Basically you can take my code and change the where statement to .NAME +" = '"+name+"'",
private Cursor getPlaylists(String playlistId){
Cursor cursor = null;
String[] projection1 = {
MediaStore.Audio.Playlists._ID,
MediaStore.Audio.Playlists.NAME
};
cursor = this.managedQuery(
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
projection1,
MediaStore.Audio.Playlists._ID+ " = "+playlistId+"",
null,
null);
startManagingCursor(cursor);
cursor.moveToFirst();
playlist_id = cursor.getString(0);
playlist_id2 = cursor.getLong(0);
if(playlist_id2 > 0){
String[] projection = {
MediaStore.Audio.Playlists.Members.AUDIO_ID,
MediaStore.Audio.Playlists.Members.ARTIST,
MediaStore.Audio.Playlists.Members.TITLE,
MediaStore.Audio.Playlists.Members._ID
};
cursor = null;
cursor = this.managedQuery(
MediaStore.Audio.Playlists.Members.getContentUri("external",playlist_id2 ),
projection,
MediaStore.Audio.Media.IS_MUSIC +" != 0 ",
null,
null);
}
cManager(cursor,2,1);
return cursor;
}
Here is a working way to get tracks from a play list. Basically it loops the cursor through all the of the playlist query, and each time it gets the id of a member (track) and using that id of the track we can get other data such as path, artist, duration, album etc.
ContentResolver contentResolver = getContentResolver();
Uri playListUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID); //playlistID is the _ID of the given playlist
MediaMetadataRetriever mr = new MediaMetadataRetriever();
Cursor cursor = contentResolver.query(playListUri, null, null, null, null);
if(cursor != null)
{
if(cursor.moveToNext()) {
do {
String track_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] trackProjection = new String[]{MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.DATA};
String selection = MediaStore.Audio.Media._ID + "=?";
String[] selectionArgs = new String[]{"" + track_id};
Cursor mediaCursor = contentResolver.query(mediaContentUri, trackProjection, selection, selectionArgs, null);
if (mediaCursor != null) {
if (mediaCursor.getCount() >= 0) {
mediaCursor.moveToPosition(0);
String song_title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String song_artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String song_album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String song_path = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
}
}
} while (cursor.moveToNext());
}
}
this is the way to get all playlist from the device
public ArrayList<String> getPlayList() {
ArrayList<String> arrayList=new ArrayList<String>();
String[] proj = {"*"};
Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
// In the next line 'this' points to current Activity.
// If you want to use the same code in other java file then activity,
// then use an instance of any activity in place of 'this'.
Cursor playListCursor= activity.managedQuery(tempPlaylistURI, proj, null,null,null);
if(playListCursor == null){
System.out.println("Not having any Playlist on phone --------------");
return arrayList;//don't have list on phone
}
System.gc();
String playListName = null;
System.out.println(">>>>>>> CREATING AND DISPLAYING LIST OF ALL CREATED PLAYLIST <<<<<<");
for(int i = 0; i <playListCursor.getCount() ; i++)
{
playListCursor.moveToPosition(i);
playListName = playListCursor.getString(playListCursor.getColumnIndex("name"));
System.out.println("> " + i + " : " + playListName );
arrayList.add(playListName);
}
if(playListCursor != null)
playListCursor.close();
return arrayList;
}