I was trying to get call logs from android device by using the code from here.
Cursor c = managedQuery(allCalls, null, null, null, null);
String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));//
and getting an exception as:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 500
Can anyone help me on this?
Thanks
Can you try this?
Pass new String[]{"*"} as a projection instead of null.
Cursor c = managedQuery(allCalls, new String[]{"*"}, null, null, null);
I got the answer after trying this code
Cursor c = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, null);
int durationIndex = c.getColumnIndex(CallLog.Calls.DURATION);
String duration="";
while (c.moveToNext()) {
duration = c.getString(durationIndex);
}
number and name is not required for me, but we can get that too using the same way.
Related
I am trying to add Events in my app using a calender.According to CalenderContract,I need to provide a constant ID each time I add an event to the calender.I don't know how to do that.
I tried using calender_ID =1 which worked on some devices and calender_ID = 3 which also worked on some devices.
I think there would be some default ID which can be used to make this work properly.
Can anyone please tell me how this can be done?
Thanks in Advance.
you can get the calender id by this following code :
String projection[] = {"_id", "calendar_displayName"};
Uri calendars;
calendars = Uri.parse("content://com.android.calendar/calendars");
ContentResolver contentResolver = c.getContentResolver();
Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);
if (managedCursor.moveToFirst()){
m_calendars = new MyCalendar[managedCursor.getCount()];
String calName;
String calID;
int cont= 0;
int nameCol = managedCursor.getColumnIndex(projection[1]);
int idCol = managedCursor.getColumnIndex(projection[0]);
do {
calName = managedCursor.getString(nameCol);
calID = managedCursor.getString(idCol);
m_calendars[cont] = new MyCalendar(calName, calID);
cont++;
} while(managedCursor.moveToNext());
managedCursor.close();
}
So you can get the calender Id at runtime and use it . you don't need hardcoded 1 or 3 column Id.
I've gotten this snippet from StackOverflow:
Cursor people = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
try{
while (people.moveToNext()) {
int nameFieldColumnIndex = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = people.getString(numberFieldColumnIndex);
System.out.println(contact + "-" + number);
}
}catch(Exception e){
System.out.println(e);
}
people.close();
When I try to read phoneNum column I get an error using String number = people.getString(numberFieldColumnIndex). Checking the column index I find that numberFiledColomnIndex = -1.
How can I get this snippet working?
Per the documentation, getColumnIndex() returns -1 when the column doesn't exist.
Q: Are you passing the correct column name? Is the spelling correct?
I have two application application A and application B. From A I am saving an image, two string values into the database by using contentProvider.
From the application B i am accessing this database and getting the image. The code which i am using is given below
byte b[] = null;
Cursor c = mContext.getContentResolver().query(allTitles, projection,null, null, null);
if (c.moveToFirst()) {
do{
if(info.activityInfo.name != null) {
if(c.getString(1).equals(info.activityInfo.name)){
b=c.getBlob(0);
}
}
} while (c.moveToNext());
}
c.close();
Here in the selection area i am giving null so i am getting all the values in the database and i have to run this loop. Instead of that I want to give a where clause to get the exact raw.
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name ="+info.activityInfo.name, null, null);
So i have changed like this but it does not worked. How i should give the selection args ? Please help.
try..
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = "+info.activityInfo.name, null, null);
or
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = ? ", new String[]{info.activityInfo.name}, null);
in case if activity_name is text
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = "+"'"+info.activityInfo.name+"'", null, null);
or
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = ? ", new String[]{"'"+info.activityInfo.name+"'"+}, null);
You need a '?' for Android to put in values for you in the WHERE clause.
The syntax should be,
Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name =?", new String[]{info.activityInfo.name}, null);
I'm trying to use the following code to grab a random mobile phone number from the contacts:
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + "NAME" + "'", null, null);
cursor.moveToFirst();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contactId, null, null);
List numbers = new ArrayList();
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_MOBILE:
numbers.add(number);
break;
}
}
Random randGen = new Random();
return (String) numbers.get(randGen.nextInt(numbers.size()));
However, running this code produces a crash on line 4, with a message saying "CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0". The crash seems to be caused by the cursor.getString() method. Does anyone know where I'm going wrong? This is using the ContactsContract in Android 2.1. Eclipse gives no errors.
Thanks!
The moveToFirst() method returns a boolean. It returns true if it was able to move to the first row and false otherwise, indicating that the query returned an empty set.
When using a cursor, you should follow something like:
if (cursor.moveToFirst()) {
do {
// do some stuff
} while (cursor.moveToNext());
}
cursor.close();
i tried to take a data from outbox sms.
Here is my code.
Cursor cursor = getContentResolver().query(
Uri.parse("content://sms/sent"), null, null, null, null);
String dateColumn = cursor.getColumnIndex("date");
String bodyColumn = cursor.getColumnIndex("body");
String addressColumn = cursor.getColumnIndex("address");
output += new StringBuilder("\nMessage to: ").append(addressColumn)
.append("\n\n").append(bodyColumn);
Toast.makeText(context, output, Toast.LENGTH_LONG).show();
But when i start the program will be error, and ask to "force close".
Can anyone help me?
You need add this command before get the cursor values:
cursor.moveToNext();