开发者

Android Preferences how to go

In one of my activity's onCreate I am using the following to get the list view. The xml layout being applied i开发者_JS百科s from xml file "country_row". Now I want to use shared preferences to change some of the layout of my listview for eg font color, background color which should be preserved. As I know I can achieve this using the shared preferences. But assuming that I know the way to define it in xml file, in this section how can I apply some of the changes say different font color or background color using the same "country_row" xml file. Or do I have to completely define new xml file? What is the way I am confused.

   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);
}


This would be quick and dirty:

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
    }
}

The clean solution is to create your own adapter and do it there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜