Selected SMS by keyword to Listview

北城以北 提交于 2019-12-11 21:05:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!