hi am getting call log details from content Provider it's working fine but output show duplicated call details also how to avoid duplicates in call logs.
current output like
id name number time
1 abc 1233 2.30 pm
2 bdc 2897 1.pm
3 abc 1233 11.30 am
4 abc 1233 11.00 am
but i don't want this output i need like
id name number time
1 abc (3) 1233 2.30 pm
2 bdc 2897 1.00 pm
how to achieve this see my code and help me
public void readCallLogs() {
Cursor callLog = getActivity().getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC");
int cid = callLog.getColumnIndex(CallLog.Calls._ID);
int cName = callLog.getColumnIndex(CallLog.Calls.CACHED_NAME);
int cNumber = callLog.getColumnIndex(CallLog.Calls.NUMBER);
int cType = callLog.getColumnIndex(CallLog.Calls.TYPE);
int cDate = callLog.getColumnIndex(CallLog.Calls.DATE);
int cDuration = callLog.getColumnIndex(CallLog.Calls.DURATION);
// looping call log cursor object
while (callLog.moveToNext()) {
String mId = callLog.getString(cid);
String mName = callLog.getString(cName);
String mNumber = callLog.getString(cNumber);
long mCallDate = callLog.getLong(cDate);
}
I think this is the matter of display....
hence once you receive the data you need to apply the filter or you need to write your own code to group the data.
NOTE: you need to be careful while choosing the Data structure (Array, Map). this can hit the performance.
Related
This is the structure and data of my table funco inside database your.db (SQLite):
Id Disp Tp
1 Ball 2
2 Light 1
3 Sea 4
This is my code:
var db = SQLiteDatabase.openDatabase(this.filesDir.path +
"/your.db",null, SQLiteDatabase.OPEN_READWRITE)
val c = db.rawQuery("Select Id, Disp, Tp From Funco Where Id<=2;",null)
var stat = c.moveToFirst()
var result=""
while (stat) {
val mId = c.getInt(c.getColumnIndex("Id"))
val mDisp = c.getString(c.getColumnIndex("Disp"))
val mTp = c.getInt(c.getColumnIndex("Tp"))
result += "$mId $mDisp $mTp | "
stat = c.moveToNext()
}
c.close()
db.close()
The result value:
1 Ball 2 | 2 Light 1 |
If I replace my second line for
val c = db.rawQuery("Select ?, ?, ? From Funco Where ?<=2;"
,arrayOf("Id","Disp","Tp","Id"))
There is no error raised, but the cursor is empty!
Why?
Update:
The #tynn answer is right. I think that the documentation is subtle.
I think that compiler shoud be throw an error and not just return empty cursor.
In the same flavor, one can write
val c = db.query("funco",arrayOf("Id","Disp","Tp"),"Id<=?",
arrayOf("2"),null,null,null)
But below code fails
val c = db.query("funco",arrayOf("Id","Disp","Tp"),"?<=?",
arrayOf("id","2"),null,null,null)
As the documentation states:
selectionArgs String: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
The question marks are there to bind different values to a precompiled SQL query. This functionality is not suitable for configuring the columns to be used in the query. Your first approach is the correct one. You could make the 2 in it dynamic though:
db.rawQuery("Select Id, Disp, Tp From Funco Where Id<=?;", arrayOf("2"))
I want to display all my records that are stored in the database
c = db.DBhelper.getChamp1(c.getCount);
//startManagingCursor(c);
int j = 0;
stateNameArray = new String[c.getCount()];
c.moveToFirst();
while(!c.isAfterLast()) {
stateNameArray[j] = c.getString(0);
j++;
Log.i("DEBUG_SQL","" + c.getString(0)+ " "+c.getString(j));
c.moveToNext();
}
//String resultat = ;
Log.i("DEBUG_SQL","" + c.getColumnIndex("article"));
I get an error when I write c.getCount – why? When I write a number like 1 or 2 or 3... that works.
And if I write
c = db.rawQuery("SELECT * FROM loan", null);
I get an error, but if I write
db.rawQuery("SELECT * FROM loan WHERE _id=1", null);
That works. Why?
At the very least, I see a problem with this log statement, where c.getString(j) doesn't make sense. And it may trigger an error as j gets larger.
Log.i("DEBUG_SQL","" + c.getString(0)+ " "+c.getString(j));
What data did you intend to access with the statement c.getString(j)?
On the getCount error. I assumed the error in the following was a typo. But is this where the error associated with getCount was located?
c = db.DBhelper.getChamp1(c.getCount);
But I shouldn't assume - you never know. It should read (add brackets to the method call).
c = db.DBhelper.getChamp1(c.getCount());
And as #Barak mentioned, what is going on with this statement?
To answer the question about your getCount isuue, you get the error because of this:
c = db.DBhelper.getChamp1(c.getCount);
You're trying to get the count of the cursor before you have it (and you're missing ()).
This would work since you have a cursor to count before you pull the next one:
c = db.getSomeCursor;
c1 = db.DBhelper.getChamp1(c.getCount());
Let us know what you're trying to achieve (I can't figure it out from the code you posted) and maybe we can be more helpful.
I'm displaying a scoreboard from a SQLite table in my app.
I want to the players so that they have the position in front of there name.
The problem i am having is that if they have equal time i want them to have equal position.
and after this i want the loop to jump over the next position. All this should be calculated and inserted into the SQLite database.
Like this:
position name time
1 George 4.00
2 Bill 5.02
2 Henry 5.02
4 Sabina 6.05
5 Heini 6.32
I'm not shure how to tackle this.
I want the loop to run just before i set my view, because the background data is changing also.
Can I use an cursor to select from my database an run a while loop? and how can i refer to the previous selection?
Thanks
Try something like this:
int position = 1;
int time, previousTime = 0;
String name;
while (cursor.moveToNext()) {
name = cursor.getString(...);
time = cursor.getInt(...);
if (time != previousTime) {
position = cursor.getPosition() + 1;
}
System.out.println(position + " " + name + " " + time);
}
I have a problem with Cursors.
I have 3 tables in my DB : facture (means invoice), vehicule (vehicle) and garage.
An invoice concerns one vehicle and one garage. When I create an invoice, I select the vehicle and the garage from spinners.
When I want to update an invoice, I need to set the item selected in these spinners.
Here is how I do :
for (int iVhc = 0; iVhc < spListeVhc.getCount(); iVhc++) {
Cursor valueVhc = (Cursor) spListeVhc.getItemAtPosition(iVhc);
long idVhc = facture.getLong(facture.getColumnIndexOrThrow("TB_VEHICULE._id"));
long idSpVhc = valueVhc.getLong(valueVhc.getColumnIndex("TB_VEHICULE._id"));
if (idSpVhc == idVhc) {
spListeVhc.setSelection(iVhc);
}
}
for (int iGar = 0; iGar < spListeGar.getCount(); iGar++) {
Cursor valueGar = (Cursor) spListeGar.getItemAtPosition(iGar);
long idGar = facture.getLong(facture.getColumnIndexOrThrow("TB_GARAGE._id"));
long idSpGar = valueGar.getLong(valueGar.getColumnIndex("TB_GARAGE._id"));
if (idSpGar == idGar) {
spListeGar.setSelection(iGar);
}
}
It works for the garage, but the problem is that, for a reason that I don't understand, the spinner of vehicles takes the same ID than the garage.
That means, if the garage selected has the ID 2 in the DB, the selected vehicle will be the vehicle with ID 2 too.
??
Here is my query to get the invoice:
public Cursor recupFacture(long idFacture){
return db.rawQuery("SELECT TB_FACTURE._id, libelle_fa, date_fa, nom_vhc, kilometrage_fa, nom_garage, remarque_fa, date_paie_fa, montant_fa, TB_VEHICULE._id, TB_GARAGE._id" +
" FROM TB_FACTURE, TB_VEHICULE, TB_GARAGE" +
" WHERE fk_vhc_fa = TB_VEHICULE._id" +
" AND fk_gar_fa = TB_GARAGE._id" +
" AND TB_FACTURE._id ="+idFacture, null);
}
And I realised that I have thi kind of mistakes in my log :
08-10 12:54:22.431: ERROR/Cursor(17072): requesting column name with table name -- TB_VEHICULE._id
And same for garage...
Thanks for your help!
EDIT :
I found a solution.
I replaced the TB_GARAGE._id and TB_VEHICULE._id by the fk at the lines :
long idVhc = facture.getLong(facture.getColumnIndexOrThrow("TB_VEHICULE._id"));
long idGar = facture.getLong(facture.getColumnIndexOrThrow("TB_GARAGE._id"));
However, I can't really explain why it works like this but not with the ID.
The prefix of the table causes a strange mistake...
Are you saving the invoice correctly? Make sure you are not accidentally saving the garage ID as vehicle ID as well.
Otherwise, how is "fracture" defined? Perhaps there is a mistake there.
I am trying to extract information from the call log of the
android. I am getting the call date that is one month back from the
actual time of call. I mean to say that the information extracted by
my code for the date of call is one mont back than the actual call
date.
I have the following in the Emulator:
I saved a contact. Then I made a call to the contact.
Code:
I have 3 ways of extracting call Date information but getting the same
wrong result. My code is as follows:
/* Make the query to call log content */
Cursor callLogResult = context.getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null, null);
int columnIndex = callLogResult.getColumnIndex(Calls.DATE);
Long timeInResult = callLogResult.getLong(columnIndex);
/* Method 1 to change the milliseconds obtained to the readable date formate */
Time time = new Time();
time.toMillis(true);
time.set(timeInResult);
String callDate= time.monthDay+"-"+time.month+"-"+time.year;
/* Method 2 for extracting the date from tha value read from the column */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
String Month = calendar.get(Calendar.MONTH) ;
/* Method 3 for extracting date from the result obtained */
Date date = new Date(timeInResult);
String mont = date.getMonth()
While using the Calendar method , I also tried to set the DayLight
SAving Offset but it didnot worked,
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
int DST_OFFSET = calendar.get( Calendar.DST_OFFSET ); // DST_OFFSET
Boolean isSet = calendar.getTimeZone().useDaylightTime();
if(isSet)
calendar.set(Calendar.DST_OFFSET , 0);
int reCheck = calendar.get(Calendar.DST_OFFSET );
But the value is not set to 0 in recheck. I am getting the wrong
month value by using this also.
Please some one help me where I am wrong? or is this the error in
emulator ??
Thanks,
Nishant Kumar
Engineering Student
Calandar's months are from 0 to 11
You need to add 1 to the month Caladar is giving you.
I know, this is strange.
Try new Date(timeInResult);