2011-09-30 58 views
0

在我的一項活動的onCreate中,我使用以下獲取列表視圖。所應用的xml佈局來自xml文件「country_row」。現在我想使用共享首選項來更改我的listview的一些佈局,例如字體顏色,應該保留的背景顏色。據我所知,我可以使用共享偏好來實現這一點。但假設我知道在xml文件中定義它的方式,在本節中,如何使用相同的「country_row」xml文件應用一些更改,說不同的字體顏色或背景顏色。或者我必須完全定義新的XML文件?我感到困惑的方式是什麼?Android首選項如何去

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     int sort = prefs.getInt("sort_pref", -1); // -1 will be the result if no preference was set before 
     if(sort == 1) 
      sortOrder = "year";//sort on year 
     else if (sort == 2) 
      sortOrder = "country";//sort on country 


     ContentResolver contentResolver = getContentResolver(); 

     Cursor c = contentResolver.query(CONTENT_URI, null, null, null, sortOrder);  
     String[] from = new String[] { "year", "country" }; 
     int[] to = new int[] { R.id.year, R.id.country };  
     SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.country_row, c, from, to);  

    setListAdapter(sca); 
} 

回答

0

這將是快速和骯髒的:

new SimpleCursorAdapter(this, R.layout.country_row, c, from, to) { 
    @Override 
    public void getView(int position, View convertView, ViewGourp parent) { 
     convertView = super.getView(position, convertView, parent); 
     View viewToModify = convertView.findViewByid(/* ID of view to modify */); 
     // apply your changes here 
    } 
} 

乾淨的解決方案是創建自己的適配器,做它。