2011-08-21 65 views
1

我已經繼承了ListActivity,我需要更改一些行的代碼背景顏色。如何做到這一點,例如第一行設置YELLOW背景?如何更改代碼中的一個項目從列表視圖的背景

public class AlarmsActivity extends ListActivity { 
    private Activity currentActivity; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setListAdapter(new ArrayAdapter<String>(this, R.layout.alarms, DATA)); 

     currentActivity = this; 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

     lv.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       Toast.makeText(getApplicationContext(), 
         ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show(); 


      } 
     }); 
    } 

    static final String[] DATA = new String[] {"Name","Last Name","Address","City" }; 


} 

回答

1

取一些顏色數組,然後在getView方法中將視圖顏色設置爲pos。例如:

private int[] colors=new int[]{0xfffff000, 0xffff0000, .....}; 

getView()寫:

view.setBackgroundColor(colors[pos]); 

這裏pos是在顏色[]

的索引值
相關問題