如何在ListView Android中加粗特定单词

时间:2020-07-07 05:23:07

标签: android listview text styles

我正在尝试在包含“:”的列表视图中加粗特定文本。我想在下图中包含“:”的加粗划线的单词加粗,但是在获取所有消息时遇到了麻烦。请在下面查看我当前的代码。 感谢您的答复。

enter image description here

ArrayList<String> list_items = new ArrayList<String>();
public  void listview_refresh(){
    arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list_items);

    ContentResolver cResolver = getContentResolver();
    Cursor smsInboxCursor = cResolver.query(Uri.parse("content://sms/inbox"),null,null,null,"date desc");
    int indexBody = smsInboxCursor.getColumnIndex("body");
    if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
    do{
        str = smsInboxCursor.getString(indexBody) ;
        arrayAdapter.add(str);

        String[] separated = str.split(":");
        String separate = separated[1];

        arrayAdapter.replaceAll(separate, "<b>" + separate + "</b>"); //errorline

    }while (smsInboxCursor.moveToNext());
}

1 个答案:

答案 0 :(得分:0)

在适配器列表中将字符串添加为HTML。我已经修改了您的代码,并添加了Html.fromHtml(..)来使文本使用HTML代码。

ArrayList<String> list_items = new ArrayList<String>();
public  void listview_refresh(){
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list_items);

ContentResolver cResolver = getContentResolver();
Cursor smsInboxCursor = cResolver.query(Uri.parse("content://sms/inbox"),null,null,null,"date desc");
int indexBody = smsInboxCursor.getColumnIndex("body");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
do{
    str = smsInboxCursor.getString(indexBody) ;
    
    String[] separated = str.split(":");
    String separate = separated[1];

    str = str.replaceAll(separate, "<b>" + separate + "</b>"); //errorline
    arrayAdapter.add(Html.fromHtml(str));
}while (smsInboxCursor.moveToNext());
}