Android 偏好设置如何进行

发布于 2024-12-07 15:00:38 字数 1127 浏览 2 评论 0原文

在我的活动之一的 onCreate 中,我使用以下方法来获取列表视图。所应用的 xml 布局来自 xml 文件“country_row”。现在我想使用共享首选项来更改列表视图的一些布局,例如应保留的字体颜色、背景颜色。据我所知,我可以使用共享首选项来实现这一点。但假设我知道在 xml 文件中定义它的方法,在本节中,我如何应用一些更改,例如使用相同的“country_row”xml 文件应用不同的字体颜色或背景颜色。或者我必须完全定义新的 xml 文件吗?我是怎么迷茫的。

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

In one of my activity's onCreate I am using the following to get the list view. The xml layout being applied is 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓酥 2024-12-14 15:00:38

这将是快速而肮脏的:

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

干净的解决方案是创建您自己的适配器并在那里进行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文