2013-08-07 41 views
0

我使用兩個獨立的.xml創建了列表視圖,每個列表視圖項目中有3個項目。 1)list_view.xml以編程方式將setColor,字體設置爲Textviews

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/ListView"> 
</ListView> 

2)list_item.xml

<?xml version="1.0" encoding="utf-8"?> 

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Text here" 
     android:id="@+id/textView" 
     android:layout_alignTop="@+id/TextView" 
     android:layout_alignLeft="@+id/TextView" 
     android:layout_centerVertical="true"/> 

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="-" 
     android:id="@+id/seperator" 
     android:layout_alignBottom="@+id/textView" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true"/> 

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="000" 
     android:id="@+id/price" 
     android:layout_alignParentTop="true" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true"/> 

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="$" 
     android:id="@+id/prefix" 
     android:layout_alignParentTop="true" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@+id/price"/> 
</RelativeLayout> 

現在,我已經創建了使用 'SimpleAdapter' 的所有物品的列表視圖。現在我想訪問列表視圖中的項目(textView,seperator,...)並以編程方式更改顏色,大小等。

這裏是我的MainActivity.java

public class MainActivity extends Activity { 


public RelativeLayout relativeLayout; 
public RelativeLayout.LayoutParams lp; 
public SimpleAdapter mList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.list_view); 
    ListView list = (ListView) findViewById(R.id.ListView); 

    //background color 
    list.setBackgroundColor(Color.YELLOW); 



    TextView itemText = (TextView)findViewById(R.id.textView); 
    itemText.setTypeface(Typeface.SANS_SERIF); 


    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 
    HashMap<String, String> map = new HashMap<String, String>(); 

    //Dummy data 
    //add all elements to map 
    map.put("text", "click to edit0"); 
    map.put("seperator", "-"); 
    map.put("price", "100"); 
    // add map elements to list 
    mylist.add(map); 

    //add all elements to map 
    map = new HashMap<String, String>(); 
    map.put("text", "click to edit1"); 
    map.put("seperator", "-"); 
    map.put("price", "102"); 
    //add all elements to map 
    mylist.add(map); 

    map = new HashMap<String, String>(); 
    map.put("text", "click to edit2"); 
    map.put("seperator", "-"); 
    map.put("price", "104"); 
    mylist.add(map); 


    //use adapter to set list items 
    mList = new SimpleAdapter(this, mylist, R.layout.list_item, 
      new String[] {"text", "seperator", "price"}, new int[] {R.id.textView, R.id.seperator, R.id.price}); 
    list.setAdapter(mList); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}的

所有工作的罰款,直到我試圖訪問列表視圖中的項目並改變它們的代碼。 我的代碼崩潰,並顯示在

TextView itemText = (TextView)findViewById(R.id.textView); 
itemText.setTypeface(Typeface.SANS_SERIF); 

一個空指針異常,我懷疑這可能是因爲我沒有到list_item.xml的的setContentView。 有沒有辦法訪問元素?如果是的話如何?

+0

您是否也可以發佈異常日誌? thx – dumbfingers

回答

0

設置顏色按下面的代碼

String text = "<font color=#8469af>By tapping Checkout, you agree to our </font> <font color=#ffffff>terms.</font>"; 
    tvTerms.setText(Html.fromHtml(text)); 
+0

'TextView'有一個設置顏色的方法:'public void setTextColor(int color)'爲什麼要使用龐大的HTML? – dumbfingers

+0

對不起...但它實際上是爲單個文本視圖設置不同的顏色 – mdDroid

+0

@mdDroid:Pelase完全讀取問題 –

0
當您嘗試調用findviewbyid你沒有創建時listItems卻又如此,如果你想設置型臉非標準字體使用

返回空指針

android:typeface="serif" 

,但如果你堅持這樣做,從列表編程 第一獲取列表項,然後

(TextView) list_item.findViewById(R.id.textView); 
0

由於塔拉指出的例外來自於this你自己的ID的TextView不存在於findViewById通話,所以如果你想以編程方式修改列表項,你應該實現自己返回null

適配器並在getView方法中應用您的修改

相關問題