Inserting data in calls content provider fails calls application - android

I am trying to insert data into call log and retrieving it back the application runs fine but no data is displayed in the activity my code is as follows.
ContentValues values = new ContentValues();
values.put(Calls.CACHED_NAME, "Chaitanya");
Uri uri = getContentResolver().insert(Calls.CONTENT_URI, values);
The uri is also receives a value but the cursor doesn't return any record.
Cursor cursor = getContentResolver().query(Calls.CONTENT_URI,
projection, null, null, null);
This cursor is then used to display the data.
After the execution of program if calls or calllog application is launched it shows exception.
What could be the possible problem with the code?
Thanks in advance

You added just a name into database. Try adding some more informations like call type, date, duration ...

Related

Confusion in ContentPorvider's CRUD operations

I am new to contenProvider and am reading a tutorial on how to do CRUD operations on UserDictionary.
For Query:
resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null);
For insert:
resolver.insert(UserDictionary.Words.CONTENT_URI, values); //ContentValues values = new ContentValues();
For update:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
long noUpdated = resolver.update(uri, values, null, null);
for delete:
long noDeleted = resolver.delete(Words.CONTENT_URI,
Words.WORD + " = ? ", new String[]{"Zaphod"});
My confusion is in update and delete operations.
In update: why is it using Words.CONTENT_URI in withAppendedId() ? shouldn't it be using UserDictionary.Words.CONTENT_URI ?
Also in delete: its not using withAppendedId(). Still why is it using Words.CONTENT_URI ?
In your example UserDictionary.Words.CONTENT_URI is what identifies the data in a provider, so the data location, the appended Id is what identifies a unique record.
So, by your examples:
1) For Query, is using UserDictionary.Words.CONTENT_URI to return all the records under this content Uri address. If you wanted to return only one specific record, from which you already know its Id, then it could also be as follows:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
resolver.query(uri, projection, null, null, null);
3) For Insert, no Id is used because the record doesn't exist yet. What the operation does is to insert a new record into the database, and the return value will be the new record Uri, which will include the new Id.
3) For Update, the appended Id is used to identify which specific record must be updated. Updating UserDictionary.Words.CONTENT_URI without a record Id would result in the update of all the records.
4) For Delete, your example is deleting all the records where the column Words.WORD has the value "Zaphod", which may result in 0, 1, or multiple records being deleted.
If you wanted to delete only 1 specific record from which you know its Id, then it would be as next:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
resolver.delete(uri, null, null);
Answering your last question, there is no difference between Words.CONTENT_URI and UserDictionary.Words.CONTENT_URI. The UserDictionary section is a qualifier, which can be removed if has been added as an import on top of the java file, this allows not having to type it all the time, but at the end both are actually the same thing and makes no difference.

Unable to show updated values from Cursor when live updating SQLite database

I am fairly new to Android and trying to learn how things work module by module. Here's what I am trying to do:
Show a word with a favorite checkbox (image). If a user taps on it then the database is updated and a column in database table stores its value (1 for checked, 0 for unchecked). I am using a cursor to retrieve values of both the word and favorite checkbox. Tapping on the favorite image correctly updates the database without any problem.
The problem I am facing is:
Unless I exit the application and start it again, the cursor doesn't fetch the recent changes made to the database. To explain it further, when I navigate to the next/previous word (using a button at the bottom of screen) the values retrieved aren't the latest ones i.e it seems like the cursor still has the old database values and not the updated ones.
I did search through Google, StackOverflow to get a concrete solution but it seems like I am not using the right search terms. I know this has something to do with updating cursor and the fact that requery is depreciated but again I have lost direction.
[EDIT] Using the below mentioned method to get Cursor:
public Cursor getWords() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = {"_id", "word", "favourite"};
String sqlTables = "word_list";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null,
null, null, null);
c.moveToFirst();
return c;
}
This method is called when user taps on favorite image to update the database:
public void setFavWord(int markFav, int wordPos) {
SQLiteDatabase db = getWritableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
ContentValues values = new ContentValues();
values.put("favourite", markFav);
db.update("word_list", values, "_id = " + wordPos, null);
}
A cursor is not dynamic; it shows a snapshot of the database at the time the query was executed.
When the database changes, you must execute the query again.
As suggested by #Sreekanth in comments section, I am updating the cursor whenever favorite image is tapped. Although it is working just fine but I think it as a workaround rather than a solution. Maybe I am wrong in saying so.

How to use own ContentProvider to other application?

I have created a my own ContentProvider by extending class ContentProvider with all its abstract methods.
I am able to use MyContentProivder in my application where I created it,but I
am not getting how can I use it in other application.
This question might have been asked many times but I am really not getting any information.
Please Help
If you have your Custom ContentProvider you can use it in other application with its URI.
To insert,
ContentValues values = new ContentValues();
values.put("title", "lalit");
values.put("isbn", "0470285818");
Uri uri = getContentResolver().insert(Uri.parse(URI), values);
To read,
Uri allTitles = Uri.parse(URI);
Cursor c = managedQuery(allTitles, null, null, null, null);
In the same way you can delete, update using query.

Does cursor copy the result set?

For example, if i use following code to query some data from database.
Uri uri = Uri.parse("content://com.android.contacts/contacts?address_book_index_extras=true");
String selection = "LEFT OUTER JOIN (select raw_contact_id, data1 from data where mimetype_id = 5) AS phone_data ON(_id = phone_data.raw_contact_id)";
Cursor c = getContentResolver().query(Contacts.CONTENT_URI, null, selection, null, null);
What i want to ask is after the query method, does database copy its result set to cursor or just make cursor something like a pointer and point to the first line of result set and query for more data when we call `moveToNext'
thanks
Yes. It is a readonly copy of the DB.
From Android Developers:
This interface provides random read-write access to the result set returned by a database query. Cursor implementations are not required to be synchronized so code using a Cursor from multiple threads should perform its own synchronization when using the Cursor.

Querying a content provider that has no records?

I am working on a method that queries a content provider using a cursor. After I delete a record, it calls the loadfromProvider method and refreshes the arraylist. The content provider normally has records in it however, when I delete all the records and the query runs automatically it throws exceptions. Here is my method:
private void loadFromProvider() {
// Clear the existing array list
EQlist.clear();
ContentResolver cr = getContentResolver();
// Return all the saved records
Cursor c = cr.query(EQProvider.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
do {
String details = c.getString(EQProvider.DETAILS_COLUMN);
String linkString = c.getString(EQProvider.LINK_COLUMN);
EQli q = new EQli(details, linkString);
addEQToArray(q);
} while(c.moveToNext());
}
c.close();
}
When I run this with no records in the content provider it throws the following:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
I think this is due to the cursor trying to parse a null value. I am trying to figure out a way that if the cursor does not come back with any records, it bypasses the rest of the code and does nothing happens.
Any help would be appreciated. Thanks
If there are no records your cursor should be empty. If the cursor is empty then c.moveToFirst() will return false and you are not going to enter the do-while loop.
Debug your application and make sure that the cursor is empty.

Categories

Resources