2011-09-30 59 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 
    } 
} 

干净的解决方案是创建自己的适配器,做它。