This question already has answers here:
Comparing phone numbers in Android
(4 answers)
Closed 8 years ago.
in my android app i have an SQLite Database with phone number (here I changed the numbers with other inexistent for privacy)!
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
I created a List containing the numbers from database and printed on the screen to see if it works and it give me back correct the numbers!
List numeri = mySQLiteAdapter.getAllNumeri();
System.out.println("num tel: " + phone);
It give me back two numbers present in database --> [3401234567, 02123456]
Then i tryed to compare the incoming number of a call with this List with this metod:
String phone = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("Incoming number - " + phone);
boolean retval = numeri.contains(phone);
if (retval == true) {
System.out.println("PHONE is contained in the list");
}
else {
System.out.println("PHONE is not contained in the list");
}
I tryed to call with my number 3401234567!
The println give me back: Incoming number: +393401234567
and
The condition give me back that "PHONE is not contained in the list!"
I tryed also to change database numbers by adding the national prefix (+39) but it don t works!
Can anyone help me? Thanks
I think it isn t duplicate because in other question, compare two string, here i want compare one string with arraylist
Did you check the return type of
mySQLiteAdapter.getAllNumeri()
if its number, then the below statement might not give the desired output as you are trying to compare the numbers with the Strings.
boolean retval = numeri.contains(phone);
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I hope somebody could give me an explaination why the below code wont work:
//Why doesnt this work
String l = myString.substring(cut, lengthLastBtn-1);
String c = myString.substring(cut, lengthLastBtn-1);
if(l==c){
Log.i(TAG, "Correct");
}
//End
//This work!
String l = "hi";
String c = "hi";
if(l==c){
Log.i(TAG, "Correct");
}
// End
// Or if i want the Vars as in the first code i have to use the if statement like this
if(l.contains(c)){
Log.i(TAG, "Correct");
}
//End
So, why cant a compare a string when i have used the substring method on it. I even see in the log for the strings that they are the same, or have the same text at least.
When you use the “==“ operator with String`s, it means a comparison between objects, not the value that objects hold.
In order to compare Strings values , you should use the built-in method equals. The result is true if the String object represents the same sequence of characters.
if(string1.equals(string2)) {
//Match
}
I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}
This question already exists:
Count the number of incoming calls (in a specific date) a certain number has done in your phone
Closed 9 years ago.
I have a phonestatelistener which retrieves the incoming call number
what i want to do is to have that certain number queried and checked in the callLog content provider and get the number of incoming calls that specific number has made on a specific date
and with that, i want to check also if that number has the most incoming calls for the specific time given, if yes, only his calls will ring, otherwise, no ring will be heard
the app actually considers the person with the most incoming calls as a frequent caller,
if he qualifies, then he will be considered an important contact and his calls will ring
my problem is, how do i do it in a query?? heeelpp!!
You can use the code as
Uri urlcall = Uri.parse("content://call_log/calls/");
Cursor receivec = null;
receivec = HomeErasor.this.getContentResolver().query(urlcall,
null, "TYPE='" + CallLog.Calls.INCOMING_TYPE + "'", null,
null);
if (c.moveToFirst()) {
do {
//here you will get all information like number and date compare same number with different date and that date should be as you want.( CALLLOG.CALL.DATE return time+date ) by comparing this you will get how many times that number called you.
String num =c.getString(c.getColumnIndex(CallLog.Calls._ID));
String queryString = "_ID=" + num;
// Log.v("_ID", queryString);
// use value
} while (c.moveToNext());
}
I'm creating an Sms app which contains a ListView in the Main Activity that displays all the conversations from the sms inbox. Each ListView row displays one conversation along with the phone number, message body and time of the message. Now instead of the phone number I want to display the contact name if it exists.
So far, for getting the contact name by phone number I found this code
private String getDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor contactLookup = context.getContentResolver().query(uri, new String[] {ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
int indexName = contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
try {
if (contactLookup != null && contactLookup.moveToNext()) {
number = contactLookup.getString(indexName);
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return number;
}
But this seems inefficient as it has to make a query for each contact name individually and lags the app. So instead I tried to get all the contact names from the phone and store it in an HashMap with the phone number as the key and the contact name as the value, so that I can get the contact name any time I want from the HashMap. But there seems to be another problem, the phone numbers are stored in many different formats, for eg:
+91 4324244434
04324244434
0224324244434
So how do I search for a phone number from the HashMap since it can be stored in many different formats?
Before you add the contact to the HashMap, use regular expression to grab the phone number. This way, no matter what format the phone number is in, the regular expression will be able to grab the appropriate number needed.
Once you grab the number, add it to the HashMap accordingly.
Hope that answers your question.
in my app i have two edit boxes for email and username. Whatever the user types in it i am trying to move it over an url as follows
http//xxxxxxx.com/id?mail=*email&user=*usernane
By this i am getting a return data from the url, this is what i am doing if network is available. But if network is not available i am storing those two values in Sqlite database and in another activity if network is available i will be fetching the above said data and i will move them to the server.
My problem is, at the time of network not available if the user tries to send two set of username and email to the server it gets stored in database. How can i store those values in an array and how can i fetch them one by one. Please help me friends
Following is the part of my code for database
off = openOrCreateDatabase("Offline.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
off.setVersion(1);
off.setLocale(Locale.getDefault());
off.setLockingEnabled(true);
final String CREATE_TABLE_OFFLINEDATA ="CREATE TABLE IF NOT EXISTS offlinedata(spotid INTEGER, username TEXT, email TEXT);";
off.execSQL(CREATE_TABLE_OFFLINEDATA);
ContentValues values = new ContentValues();
values.put("id", millis);
values.put("name", username);
values.put("mail", email);
off.insert("offlinedata", null, values);
Cursor con = off.rawQuery("select * from offlinedata" , null);
if (con != null )
{
if (con.moveToFirst())
{
do
{
int spotid = con.getInt(con.getColumnIndex("id"));
String first = con.getString(con.getColumnIndex("username"));
String middle = con.getString(con.getColumnIndex("email"));
}
while (con.moveToNext());
}
}
off.close();
Please help me friends....
From looking at your sample code, it seems like you're storing them properly(ish), and you've managed an exhaustive job fetching them in a really narrow scope, so make first and middle more globalish and since you have two strings available, put them in an array.
Though I must say if this is your actual code it probably won't work the way you want this whole offline thing to work.