如何使我的String Builder标签ID,名称,地址加粗

我正在显示数据库列表,我希望标签为粗体。

   Cursor cursor = GetAllData();
           StringBuilder stringBuilder =new StringBuilder();
           while(cursor.moveToNext()){
               stringBuilder.append("Id :" + cursor.getString(0)+ " | " +"Name :"+ 
     cursor.getString(1)+" | "+"Address :" + cursor.getString(2)+" | "+"Phone :" + 
      cursor.getString(3)+"\n"+"--------------------------------------------------"+"\n");
           }
           textView.setText(stringBuilder);

也就是说,Id,Name,Address必须加粗以使其加粗。

评论
  • hea
    hea 回复

    You are setting a textView. Add HTML bold tags. Also, that is terrible code. You are nesting StringBuilder (because String + String uses another StringBuilder). Also, it's a bad idea to assume the line separator is \n. Fixing it should be simple enough. Something like,

    Cursor cursor = GetAllData();
    StringBuilder sb = new StringBuilder();
    while (cursor.moveToNext()) {
        sb.append("<b>Id</b> :").append(cursor.getString(0));
        sb.append(" | <b>Name</b> :").append(cursor.getString(1));
        sb.append(" | <b>Address</b> :").append(cursor.getString(2));
        sb.append(" | Phone :").append(cursor.getString(3));
        sb.append(System.lineSeparator());
        sb.append("--------------------------------------------------");
        sb.append(System.lineSeparator());
    }
    textView.setText(Html.fromHtml(sb));