在运行时将项目添加到 ListPreference
我正在尝试为我正在开发的 Android 项目创建一个首选项屏幕。
我已经创建了一个 XML 配置文件,一直用于调试,但现在我想创建一种更加用户友好的方式来设置应用程序的首选项,并决定使用首选项屏幕。
作为该项目的一部分,我有一个 RSS Feed 阅读器,我希望能够为其设置多个 URL feed,但我不知道如何设置。我真正需要的是能够让我向首选项中配置的 URL 列表添加新 URL(或删除 URL)的东西。
作为示例,这是我一直用于调试事务(获取数据)的 xml 的 rss 部分:
<source type="rss">
<active>true</active>
<target_url>http://feeds.tsf.pt/TSF-Ultimas</target_url>
<target_url>http://www.arcamax.com/thefunnies/garfield/rss</target_url>
</source>
I am trying to create a preference screen for an Android project I'm developing.
I have created a XML config file, which I've been using for debug, but now I want to create a more user friendly way to set the preferences for the application, and decided to go for the Preference Screen.
As part of this project, I have a RSS Feed reader, for which I want to be able to set more than one URL feed, but I can't figure out how. What I really need is something that will let me add a new URL (or remove one) to a list of configured URLs in Preferences.
As an example, here's the rss part of the xml I've been using for debug matters (fetching data):
<source type="rss">
<active>true</active>
<target_url>http://feeds.tsf.pt/TSF-Ultimas</target_url>
<target_url>http://www.arcamax.com/thefunnies/garfield/rss</target_url>
</source>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我成功了。我创建了一个名为 AddButtonPreference 的新首选项,它扩展了 Preference,它基本上创建了一个按钮并将其 OnClickListener 指向创建此首选项的 Activity。
为了创建列表,我迭代目标列表以将 EditTextPreferences 添加到 PreferenceScreen,迭代它们后,我添加 buttonPreference。然后我刚刚实现了 onClick,在单击时向该屏幕添加一个新的 EditTextPreference,并将按钮移到末尾。
I nailed it. I created a new preference called AddButtonPreference, extending Preference, that basically creates a button and points its OnClickListener to the Activity that creates this preference.
To create the list, I iterate over my target list to add EditTextPreferences to the PreferenceScreen, and after iterating them, I add the buttonPreference. Then I just implemented the onClick, to add a new EditTextPreference to that screen when clicked, and move the button to the end.