I am making an application which search messages in the inbox based on sender number. Here is my code:
public void onClick(View v)
{
Toast.makeText(this, "search", Toast.LENGTH_LONG).show();
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
String[] colList = {"address", "body"};
Cursor c = getContentResolver().query(SMS_INBOX, colList, null, null,"DATE desc");
String yz= String.valueOf(c.getCount());
Toast.makeText(this, yz, Toast.LENGTH_LONG).show();
if(c.moveToFirst())
{
Toast.makeText(this, searchtxt.getText(), Toast.LENGTH_LONG).show();
for(int i=0;i<c.getCount();i++)
{
String number=c.getString(c.getColumnIndexOrThrow("address")).toString();
boolean match = number.toUpperCase().indexOf(searchtxt.getText().toString()) != -1;
if (match)
{
Toast.makeText(this, String.valueOf(i), Toast.LENGTH_LONG).show();
String body= c.getString(c.getColumnIndexOrThrow("body")).toString();
tv.setText(tv.getText() + "\n" + body);
}
c.moveToNext();
}
}
else
{
Toast.makeText(this, "Inside else", Toast.LENGTH_LONG).show();
}
c.close();
}
The above code works fine but it retrieves all the messages in the inbox. I want that it should retrieve only those messages which match the sender number. For that I tried a query using LIKE clause, but with that it returns 0 records. What is the correct way of using the LIKE clause. Here is the code that I tried using LIKE
String[] colList = {"address", "body"};
String[] argList = {"'%"+searchtxt.getText().toString()+"%'"};
Cursor c = getContentResolver().query(SMS_INBOX, colList, "address LIKE ?", argList,"DATE desc");
String yz= String.valueOf(c.getCount());
Toast.makeText(this, yz, Toast.LENGTH_LONG).show();
Please help me out....Thank you...
Open your query in Design view. In the Criteria cell of the field you want to use, enter Like, followed by a pair of double quotes. For example: Like "".
The SQL Like is used when we want to return the row if specific character string matches a specified pattern. The pattern can be a combination of regular characters and wildcard characters. To return a row back, regular characters must exactly match the characters specified in the character string.
Description. The SQL LIKE condition allows you to use wildcards to perform pattern matching in a query. The LIKE condition is used in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.
The SQL LIKE OperatorThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.
There is two joker : "%" and "_".
"%" is replacing any string (multiple characters),
"_" is replacing one (and only one) character.
For example :
LIKE 'B%'
let you find everything which starts with "B". (like "BATMAN");
LIKE '%B%'
let you find everthing which contains "B". (like "ABBA");
LIKE'_B%'
let you find everything which contains ONE caracter, then a "B". (like "ABERCROMBIE)".
if you want to retrieve a string which contains "%" or "_", you may use an escape caracter :
LIKE '%#_B%' ESCAPE #
let you find any strings which contains one "_" followed by a "B". (like "TEST_BATMAN").
I hope that helped.
Al_th
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With