I'm building an app where I need the user to select some "favorite" contacts and a phone number. I'm able to select a contact using
startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), PICK_CONTACT_REQUEST);
and I'm able to extract all the information I need.
I then proceed to save the _id of the contact into my own database. My plan is to later list all the "favorited" contacts and display the name and phonenumber in a listview.
I want to save the contact id instead of the name and number so my listview will reflect any changes the user makes to his or her contacts.
But now I'm stuck. I don't know how to transform my table with contacts ids into a table with contact names.
I would like to something like this
my_table LEFT OUTER JOIN contacts_table ON (my_table.contact_id = contacts_table._id)
What you will want to do here is to store those IDs then when you want to pull the names from the user's address book, you'll have to cross reference the IDs with the contacts intent you have store in order to filter them out.
Basically you cant join a non-existent table (contacts_table).
You could probably just save the contect IDs however you wish, and then loop through the contact IDs and look up the contacts one-by-one extracting the data you need. If that's quick enough you wouldn't need any SQL joins ("optimize by need").
Perhaps its even quicker to use some Google API and use HTTP/XML to fetch the contacts (perhaps cache them for a while as well).
Related
I'm newbie to android and am learning the basics of kotlin/Room. As I head down the rabbit hole of data classes, DAOs joining tables, I am trying to understand why so many table/interfaces are used where multiple queries could achieve the same outcome?
Example: an application contains an address book of business contacts. A user can select one or more contacts for their address book from a large list. The aim is to only populate the users address book with the information of the contacts they have selected.
This would require:
A User table: containing unique user information for many users (userId, name, address...etc.)
A Contacts table: containing unique contact information for many contacts (contactId, name, address...etc.)
A joining table: containing the contact selections of each user (userId and contactId)
A UserWithContacts table: containing embedded user objects with a relation to the contact object in the joinging table.
The purpose of the UserWithContacts table as I understand it is to query directly and get a list of contact objects that have been selected by a specific user.
This structure, with adjoining DAOs, makes for a number of classes/interfaces/etc to keep track of.
Why is it better to take this approach and not simply query the joining table for a list of contactIDs based on a given userId, followed by a second query that returns the contact objects from the contact table with contact information? This would not required the additional UserWithContacts table or DAO interface.
For Example:
//Get a list of contactId's that have a common UserId
#Query("SELECT contactId FROM joiningTable WHERE userId =:userId")
fun getAllUserContactIds(userId: String): List<String>
//Get a list of Contacts objects from are contained within a list of Contact Ids
#Query("SELECT * FROM contactTable WHERE contactId IN (:contactIds)")
fun getAllUserContacts(contactIds: List<String>): List<Contact>
This pair of queries provides an equivalent list of Contact objects that can be used to populate the address book.
Can anyone describe why it is preferred to use multiple classes/interfaces instead of multiple queries?
Thanks in advance for your thoughts.
Ok, I have a database with id column as timestamp
I made an activity list from the db.
I want to manage the db (delete rows) using the list, but the thing is I don't want to
View the whole timestamp, in every row I'll put only the time with some info and
I want to group the list ,as in contacts grouped by alphabet, by the date.
First, how can I make group in an activity list? (Making groups to the output list not the db)
Second, what is the best way to implement this? When user chooses an item and confims delete
I should delete it from the db but I have only patial timestamp...
(My only link to the db is the timestamp - I don't actually know where to store it in the list and I don't want to put it as a string in the text view, do a substring to get it back - is there another way to do this?)
I tried to search tthe web for some examples but I only found a simple ones.
Thnx :-)
?
I think what you're trying to do is create a database of tasks identified by a timestamp. You probably don't want to use a timestamp as a unique ID for the row. Instead, use an integer and qualify it as "PRIMARY KEY" when you create the database.
group the list? I'm not sure why you want to do this in the structure of the database. It's more common to group the list in the output, and leave the db itself in as flat a structure as possible.
Retrieve the primary key when you display a list of tasks. When the user clicks a task, use the primary key to choose the task to delete. You don't have to display the primary key; it serves as a behind-the-scenes "link" between the displayed info and the db row.
http://www.vogella.com/articles/AndroidListView/article.html
I should use cursor adapter for managing db.
And this one for grouping a list:
http://code.google.com/p/android-amazing-listview/
Thnx for the efforts
as far as you might know, there are contacts (aggregate contacts) which are formed by aggregation of two or more raw contacts in Android V2.x
is it possible to identify all the raw contacts from which a single aggregate contacts is formed through a query on the ContactsContract.Contacts or is there a way to identify these
contacts at all?
i could not find any flag or database field that tells me that this aggregate contacts is linked with these raw contacts.
any suggestions?
You can check AggregationExceptions.CONTENT_URI Table where relationship type are AggregationExceptions.TYPE_KEEP_TOGETHER, AggregationExceptions.TYPE_KEEP_SEPARATE, etc.
and you can find Raw_contact_id1 and raw_contact_id2.
Example of data into database. Lets say 1,2,3,4 are in relation so you can find following pairs.
Raw_contact_id1 raw_contact_id2 Relationship type
1-> 2, 1->3, 1->4, 2->3, 2->4, 3->4
A Contact cannot be created explicitly. When a raw contact is inserted, the provider will first try to find a Contact representing the same person. If one is found, the raw contact's CONTACT_ID column gets the _ID of the aggregate Contact. If no match is found, the provider automatically inserts a new Contact and puts its _ID into the CONTACT_ID column of the newly inserted raw contact.
So, while reading all the contacts one by one we can take its _ID value and can retrieve all the contacts from raw_contacts where _ID matches with raw_contacts.CONTACT_ID.
If the count is greater than 1 then we can conclude that it is linked with those numbers of contacts else it is not linked with any other contact.
I am making an Android app that will use a friends list. As far as I know, you can't store an array in a MySQL table, so it seems that I will have to make a separate table from my "Users" table and call it, say, "Friends". This table will just have Friend1 and Friend2.
My question is the preferred way to store this table. I can use the UserName field (string) from my "Users" table to store them, or I could use the UserID field (integer) from my "Users" table. Using the ID would make the table smaller because the small integers take up more space than the string, but at the same time, I access this data mainly using the UserName field (so I have to query the Users table to get the UserID from the Users table).
Which method is preferred for a MySQL table? Using the users name directly so I do not have to find the UserID from the Users table, or saving the table as two integers, and querying to find the ID from the UserName? Thanks.
Store two userID keys from the users table.
Let's say that you change a name of a contact from "Guy from bar" to "Mr. McMoneypants". If this contact was a friend, it will still show up as "Guy from bar" even after the change.
Try to keep data from living in multiple places.
The preferred method is to use the UserID from the Users table in the Friends table as a way to reference that user. That way, as Phillip says, the User can change their name and you only have to change it in one place. Plus, as you say, your Friends table will take up less space with a numeric column as compared to a string column.
And in regards to "(so I have to query the Users table to get the UserID from the Users table)", the following query is not too cumbersome:
SELECT FriendName
FROM Users Natural Left Join Friends
WHERE UserName = 'Ralph';
As far as your query is concerned, you never had to deal with the UserID column.
That's not that much harder than your method:
SELECT FriendName
FROM Friends
WHERE UserName = 'Ralph';
I retrieve the display name of a user via the ContactsContract API in Android.
Now I want to retrieve the given(first) name of this user. There are specific Rows in the data table that contain the name of the user. The problem is that there are multiple rows for every user because of the synchronization and aggregation of contacts.
The Contacts table documentation states that there should be column containing the id of the raw contact that contributes the primary name for this user. But there is no constant name for this column defined and I couldn't find this column inside my data table.
How do I retrieve the Id of the raw contact that contributes the DisplayName to a contact?
If I understood correctly, the "IS_PRIMARY" or the "IS_SUPER_PRIMARY" flag in the RawContactsEntity-table may solve your problem.