我正在尝试将颜色和粗体添加到与ArrayList项目/值中的句子匹配的单词上。到目前为止我所做的事情如下......所以我有一个名为filter" Hello"现在我想遍历ArrayList itemList并找到" Hello"从ArrayList项目中,使其变为粗体和蓝色。
String filter = "Hello";
String itemValue = "I just want to say Hello world! Hello";
itemList = new ArrayList<String>();
for(int i = 0; i<10; i++)
{
itemList.add("I just want to say Hello world! Hello");
}
sentence = new ArrayList<String>();
for(int j=0; j<itemList.size(); j++)
{
int startPos = itemList.get(j).toString().toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US));
int endPos = startPos + filter.length();
if (startPos != -1) // This should always be true, just a sanity check
{
Spannable spannable = new SpannableString(itemValue);
ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(spannable);
sentence.add(itemValue);
ListView lv = (ListView)findViewById(R.id.listView1);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1, android.R.id.text1, sentence);
MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, spannable, counterArrayList);
lv.setAdapter(adapter);
}
else
tv.setText(itemValue);
}
它无法正常工作,请你帮助我
答案 0 :(得分:3)
你应该让另一个构造函数传递另一个参数String filter
,它将包含你的字符串为粗体。
将其分配给arrayAdapter
现在在显示视图时
你应该在每个句子中替换filter
。即sentence.replace(filter, "<b>" + filter + "</b>")
并将其显示在textView中,如myTextView.setText(Html.fromHtml(sentence));
之后您需要做的就是重置适配器