2014-01-24 34 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);