Android的加入项列出preference programaticly [英] Android adding item to list preference programaticly

查看:195
本文介绍了Android的加入项列出preference programaticly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我一直在寻找的地方每隔没有找到一个答案。

I feel like i have been searching every where without finding an answer to this.

我有一个列表preference

I have a list preference

     <ListPreference
            android:key="signedUpCompetetion"
            android:entries="@array/listArray"
             android:entryValues="@array/listValues"
                />

通过下面的条目和entryValues​​:

With the following entries and entryValues:

    <resources>
<string-array name="listArray">
    <item></item>
    <item></item>
    <item></item>
</string-array>
<string-array name="listValues">
    <item></item>
    <item></item>
    <item></item>
</string-array>
</resources>

所以说,在我的活动之一,我想的东西添加到列表中。

So say that in one of my activities i want to add something to the list.

下面是一个尝试,但它似乎没有工作:

The following is an attempt but it doesnt seem to work:

this.appPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
this.prefEditor = appPrefs.edit();
ListPreference list = (ListPreference) getSharedPreferences("signedUpCompetetion", MODE_PRIVATE);
list.setEntries("This is a test");

任何人能告诉我如何添加到列表preference?

Anyone able to tell me how to add to a listpreference?

更新
由外观上来看这似乎工作

Update By the looks of it this seems to work

        PreferenceScreen root = this.getPreferenceScreen();
    ListPreference list = (ListPreference) root.findPreference("signedUpCompetetion");
    CharSequence[] entries = { "One", "Two", "Three" };
    CharSequence[] entryValues = { "1", "2", "3" };
    list.setEntries(entries);
    list.setEntryValues(entryValues);

然而,有1的问题!如果我重新启动应用程序列表不是preserved。这意味着该列表为空!

However there is 1 problem! if i restart the application the list is not preserved. which means that the list is empty!

推荐答案

使用<一个href=\"https://developer.android.com/reference/android/$p$pference/List$p$pference.html#setEntries%28int%29\"相对=nofollow> setEntries(INT) 和<一个href=\"https://developer.android.com/reference/android/$p$pference/List$p$pference.html#setEntryValues%28int%29\"相对=nofollow> setEntryValues​​(INT)

    list.setEntries(R.array.listArray);
    list.setEntryValues(R.array.listValues);

更新如何坚持数据:

您可以坚持几种方式中的数据最简单的一种是叫<一个href=\"https://developer.android.com/reference/android/$p$pference/$p$pference.html#setPersistent%28boolean%29\"相对=nofollow> setPersistent(真)

You can persist the data in several ways, the simplest one would be to call setPersistent(true):

    list.setPersistent(true);

这篇关于Android的加入项列出preference programaticly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆