2012-03-21 98 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中的確切代碼來準確更改文本顏色。只是谷歌它。

這將解決您的問題。