2012-03-21 68 views
1

我的问题是如何更改我的列表视图中每个单元格的前6个字符的背景颜色(或文本颜色)? (前6个字符包含日期实例。“简01:等等等等等等数据资料...”)Android ArrayAdapter风格

我有我的网页上一个列表视图,它会从一个SOAP Web服务调用

 SoapObject oResponse = (SoapObject)soapEnvelope.getResponse(); 
     ArrayList<String> oStringList = new ArrayList<String>(); 
     for(int i = 0; i < oResponse.getPropertyCount(); i++) 
     oStringList.add(oResponse.getProperty(i).toString()); 
     lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, oStringList)); 
动态填充

任何指导将不胜感激。

回答

0

设置TextView的文字时,您将使用Spannable,因此您可以更改范围的颜色。这里有一个例子,将前景色设置为红色,以显示六个字符。你将在getView中使用这段代码,电视是TextView

TextView tv = ... 
String text = "Jan 01: Blah blah blah data data..."; 
int startColor = 0; 
int endColor = 6; 
int color = 0xffff0000; 
Spannable spannable = new SpannableString(text); 
spannable.setSpan(new ForegroundColorSpan(color), startColor, endColor, 0); 
tv.setText(spannable); 
+0

感谢您的快速回复。不知道textView如何连接(或填充)到列表视图?我目前使用lv.setAdapter填充listView(新ArrayAdapter (this,android.R.layout.simple_list_item_1,zarray)); – AhabLives 2012-03-21 02:50:19

0

dldnh的答案是正确的,但另一种方法是必须的TextView的彼此相邻,一个日期,一个为你的实际文本。因此,像这样:

<LinearLayout android/+id="rowLayout" android:width="wrap_content" android:height="wrap_content" android:orientation="horizontal"> 
    <TextView android/+id="txtDate" android:text="Jan 01" android:textColor="@color/red" android:width="wrap_content" android:height="wrap_content"/> 
    <TextView android/+id="txtItem" android:text="blah blah blah" android:layout_marginLeft="5dp" android:width="wrap_content" android:height="wrap_content"/> 
</LinearLayout> 

所以在你的适配器,覆盖getView(int position, View v)(我敢肯定,这就是它),你可以在里面设置的文字。

0

在想你应该让自己的ArrayAdapter布局。

在这个新的布局中,listview的每个数据都有两部分:(两个textview),并且你把它放在horizontal linearlayout。第一个textview,你可以像你说的那样使其属性。

0

我会给你一个简单的解决方案。只需写下代码即可更改html中文本块的颜色。有些东西就像

String s = "<text color=red>abcdef</text>"+"continuition part will be placed here"; 

TextView Object.setText(Html.toHtml(s)); 

我不知道Html中的确切代码来准确更改文本颜色。只是谷歌它。

这将解决您的问题。