2014-01-24 79 views
1

,我的工作在應用程序是非常沉重的文字,有一對夫婦可擴展的列表,所以我用多維數組來填寫孩子的意見。 我想是這樣的文本的粗體某些部分: 實施例1:這是一個例子。 因此,要做到這一點,我有這個在我的strings.xml:Html.fromHtml()文本格式和多維數組

<string name="example"><![CDATA[<b>Example 1:</b> This is an example.]]></string> 

在我的適配器:

CharSequence example = Html.fromHtml(getString(R.string.example); 

想象噸的,然後我適合他們到我的數組:

private CharSequence [][] childview = { 
    {example, example1, example2, example3}; 
    {example4, example5, example6, example7}; 
}; 

的問題是,我們有了一個更好的辦法做到這一點,而不是創造一大堆的變量,對不對?我可以將我的字符串變成一個字符串數組,並使用Html.fromHtml格式將它們放入多維數組中嗎?

+0

這更多的是[]比[] [] – njzk2

+0

我的壞,編輯。 – Racepace

回答

0

你可以寫一個小工具方法:

public CharSequence [][] fromRes(int[][] resIdMatrix) { 
    CharSequence[][] result = new CharSequence[resIdMatrix.length][]; 
    int i = 0; 
    for (int[] resIds : resIdMatrix) { 
     CharSequence[] row = result[i++] = new CharSequence[resIds.length]; 
     int j = 0; 
     for (int id : resIds) { 
      row[j++] = Html.fromHtml(getString(id)); 
     } 
    } 
    return result; 
} 

然後用資源ID的二維數組調用它:

int[][] resIds = { 
    {R.string.example, R.string.example1, R.string.example2, R.string.example3}, 
    {R.string.example4, R.string.example5, R.string.example6, R.string.example7}, 
}; 
CharSequence[][] childview = fromRes(resIds);