问题
How to select sms by keyword from body to listview?
Current progress: sms are shown in listview, but all sms are displayed. I just want to display only selected sms by keyword. This is my code.
public List<String> getSMS() {
List<String> list = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cursor = null;
try {
cursor = getContentResolver().query(uriSMSURI, null, null, null,
null);
} catch (Exception e) {
e.printStackTrace();
}
try {
for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor
.moveToNext()) {
String body = cursor.getString(cursor.getColumnIndex("body"));
list.add(body);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
回答1:
If you add body content to listview, then why not compare with keyword before adding?
String keyword = "hello"
try {
for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor
.moveToNext()) {
String body = cursor.getString(cursor.getColumnIndex("body"));
if(body.toLowerCase().contains(keywoed.toLowerCase()))
list.add(body);
}
} catch (Exception e) {
e.printStackTrace();
}
I have used toLowerCase() for case insensitive compare.
Hope it helps.
来源:https://stackoverflow.com/questions/25987068/selected-sms-by-keyword-to-listview